wppaste
WordPress

sanitize_user_field( string $field, mixed $value, int $user_id, string $context ): mixed

Since
2.3.0
Source
wp-includes/user.php:1898
Sanitizes user field based on context.

Description

Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' when calling filters.

Compatibility

WordPress
since 2.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$fieldstring
The user Object field name.
$valuemixed
The user Object value.
$user_idint
User ID.
$contextstring
How to sanitize user fields. Looks for 'raw', 'edit', 'db', 'display', 'attribute' and 'js'.

Return value

mixed
Sanitized value.

Performance profile

How much work a call to sanitize_user_field() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–52

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
6 hooks

Third-party callbacks on 'edit_{$field}', 'edit_user_{$field}', 'pre_{$field}' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_user_field() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9–15none
$context !== "raw" && $context !== "edit" && $field !== "user_url" && $context !== "attribute" && $context !== "js"32–40apply_filters()
$context !== "raw" && $field !== "user_url"35–44apply_filters(), esc_attr()
$context !== "raw" && $context !== "edit" && $field !== "user_url" && $context !== "attribute" && $context === "js"36–44apply_filters(), esc_js()
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context !== "attribute" && $context !== "js"36–44apply_filters(), esc_url()
$context !== "raw" && $context === "edit" && $field === "description" && $field !== "user_url" && $context !== "attribute" && $context !== "js"37–44apply_filters(), esc_html()
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context === "attribute"39–47apply_filters(), esc_url(), esc_attr()
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context !== "attribute" && $context === "js"40–48apply_filters(), esc_url(), esc_js()
$context !== "raw" && $context === "edit" && $field !== "description" && $field !== "user_url" && $context === "attribute"40–47apply_filters(), esc_attr(), esc_attr()
$context !== "raw" && $context === "edit" && $field === "description" && $field !== "user_url" && $context === "attribute"40–47apply_filters(), esc_html(), esc_attr()
$context !== "raw" && $context === "edit" && $field !== "description" && $field !== "user_url" && $context !== "attribute" && $context === "js"41–48apply_filters(), esc_attr(), esc_js()
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context !== "attribute" && $context !== "js"41–48apply_filters(), esc_attr(), esc_url()
6 further outcomes, up to 52 instructions
$context !== "raw" && $context === "edit" && $field === "description" && $field !== "user_url" && $context !== "attribute" && $context === "js"41–48apply_filters(), esc_html(), esc_js()
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context !== "attribute" && $context !== "js"41–48apply_filters(), esc_html(), esc_url()
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context === "attribute"44–51apply_filters(), esc_attr(), esc_url(), esc_attr()
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context === "attribute"44–51apply_filters(), esc_html(), esc_url(), esc_attr()
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context !== "attribute" && $context === "js"45–52apply_filters(), esc_attr(), esc_url(), esc_js()
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context !== "attribute" && $context === "js"45–52apply_filters(), esc_html(), esc_url(), esc_js()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1069–5214
8.51069–5214
8.41069–52145 fewer instructions than PHP 8.3
8.31119–5714
8.21119–5714
8.11119–57141 fewer instruction than PHP 7.4
7.41129–5814

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 6

6 hooks fire while sanitize_user_field() runs, in this order:

  1. apply_filters( edit_{$field} )filterline 1918 (+20 into the body)

    Filters the value of a specific post field to edit.

  2. apply_filters( edit_user_{$field} )filterline 1932 (+34 into the body)

    Filters a user field value in the 'edit' context.

  3. apply_filters( pre_{$field} )filterline 1943 (+45 into the body)

    Filters the value of a specific post field before saving.

  4. apply_filters( pre_user_{$field} )filterline 1956 (+58 into the body)

    Filters the value of a user field in the 'db' context.

  5. apply_filters( {$field} )filterline 1963 (+65 into the body)

    Filters the value of a specific post field for display.

  6. apply_filters( user_{$field} )filterline 1978 (+80 into the body)

    Filters the value of a user field in a standard context.

Uses · 6

Used by · 2

Source code

function sanitize_user_field( $field, $value, $user_id, $context ) {	$int_fields = array( 'ID' );	if ( in_array( $field, $int_fields, true ) ) {		$value = (int) $value;	} 	if ( 'raw' === $context ) {		return $value;	} 	if ( ! is_string( $value ) && ! is_numeric( $value ) ) {		return $value;	} 	$prefixed = str_contains( $field, 'user_' ); 	if ( 'edit' === $context ) {		if ( $prefixed ) { 			/** This filter is documented in wp-includes/post.php */			$value = apply_filters( "edit_{$field}", $value, $user_id );		} else { 			/**			 * Filters a user field value in the 'edit' context.			 *			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.			 *			 * @since 2.9.0			 *			 * @param mixed $value   Value of the prefixed user field.			 * @param int   $user_id User ID.			 */			$value = apply_filters( "edit_user_{$field}", $value, $user_id );		} 		if ( 'description' === $field ) {			$value = esc_html( $value ); // textarea_escaped?		} else {			$value = esc_attr( $value );		}	} elseif ( 'db' === $context ) {		if ( $prefixed ) {			/** This filter is documented in wp-includes/post.php */			$value = apply_filters( "pre_{$field}", $value );		} else { 			/**			 * Filters the value of a user field in the 'db' context.			 *			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.			 *			 * @since 2.9.0			 *			 * @param mixed $value Value of the prefixed user field.			 */			$value = apply_filters( "pre_user_{$field}", $value );		}	} else {		// Use display filters by default.		if ( $prefixed ) { 			/** This filter is documented in wp-includes/post.php */			$value = apply_filters( "{$field}", $value, $user_id, $context );		} else { 			/**			 * Filters the value of a user field in a standard context.			 *			 * The dynamic portion of the hook name, `$field`, refers to the prefixed user			 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc.			 *			 * @since 2.9.0			 *			 * @param mixed  $value   The user object value to sanitize.			 * @param int    $user_id User ID.			 * @param string $context The context to filter within.			 */

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.