sanitize_user_field( string $field, mixed $value, int $user_id, string $context ): mixed
- Since
- 2.3.0
- Source
wp-includes/user.php:1898
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
- Scaling
- Constant
- Instructions
- 9–52
- Plugin surface
- 6 hooks
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.
Third-party callbacks on 'edit_{$field}', 'edit_user_{$field}', 'pre_{$field}' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9–15 | none |
$context !== "raw" && $context !== "edit" && $field !== "user_url" && $context !== "attribute" && $context !== "js" | 32–40 | apply_filters() |
$context !== "raw" && $field !== "user_url" | 35–44 | apply_filters(), esc_attr() |
$context !== "raw" && $context !== "edit" && $field !== "user_url" && $context !== "attribute" && $context === "js" | 36–44 | apply_filters(), esc_js() |
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context !== "attribute" && $context !== "js" | 36–44 | apply_filters(), esc_url() |
$context !== "raw" && $context === "edit" && $field === "description" && $field !== "user_url" && $context !== "attribute" && $context !== "js" | 37–44 | apply_filters(), esc_html() |
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context === "attribute" | 39–47 | apply_filters(), esc_url(), esc_attr() |
$context !== "raw" && $context !== "edit" && $field === "user_url" && $context !== "attribute" && $context === "js" | 40–48 | apply_filters(), esc_url(), esc_js() |
$context !== "raw" && $context === "edit" && $field !== "description" && $field !== "user_url" && $context === "attribute" | 40–47 | apply_filters(), esc_attr(), esc_attr() |
$context !== "raw" && $context === "edit" && $field === "description" && $field !== "user_url" && $context === "attribute" | 40–47 | apply_filters(), esc_html(), esc_attr() |
$context !== "raw" && $context === "edit" && $field !== "description" && $field !== "user_url" && $context !== "attribute" && $context === "js" | 41–48 | apply_filters(), esc_attr(), esc_js() |
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context !== "attribute" && $context !== "js" | 41–48 | apply_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–48 | apply_filters(), esc_html(), esc_js() |
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context !== "attribute" && $context !== "js" | 41–48 | apply_filters(), esc_html(), esc_url() |
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context === "attribute" | 44–51 | apply_filters(), esc_attr(), esc_url(), esc_attr() |
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context === "attribute" | 44–51 | apply_filters(), esc_html(), esc_url(), esc_attr() |
$context !== "raw" && $context === "edit" && $field !== "description" && $field === "user_url" && $context !== "attribute" && $context === "js" | 45–52 | apply_filters(), esc_attr(), esc_url(), esc_js() |
$context !== "raw" && $context === "edit" && $field === "description" && $field === "user_url" && $context !== "attribute" && $context === "js" | 45–52 | apply_filters(), esc_html(), esc_url(), esc_js() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 106 | 9–52 | 14 | |
| 8.5 | 106 | 9–52 | 14 | |
| 8.4 | 106 | 9–52 | 14 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 111 | 9–57 | 14 | |
| 8.2 | 111 | 9–57 | 14 | |
| 8.1 | 111 | 9–57 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 112 | 9–58 | 14 |
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:
- apply_filters( edit_{$field} )filterline 1918 (+20 into the body)
Filters the value of a specific post field to edit.
- apply_filters( edit_user_{$field} )filterline 1932 (+34 into the body)
Filters a user field value in the 'edit' context.
- apply_filters( pre_{$field} )filterline 1943 (+45 into the body)
Filters the value of a specific post field before saving.
- apply_filters( pre_user_{$field} )filterline 1956 (+58 into the body)
Filters the value of a user field in the 'db' context.
- apply_filters( {$field} )filterline 1963 (+65 into the body)
Filters the value of a specific post field for display.
- apply_filters( user_{$field} )filterline 1978 (+80 into the body)
Filters the value of a user field in a standard context.
Uses · 6
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_html()Escaping for HTML blocks.
- esc_attr()Escaping for HTML attributes.
- esc_url()Checks and cleans a URL.
- esc_js()Escapes single quotes, `"`, ``, `&`, and fixes line endings.
Used by · 2
- WP_User::__get()Magic method for accessing custom fields.
- sanitize_user_object()Sanitize every user field.
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.
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.