sanitize_user_object( object|array $user, string $context = 'display' ): object|array
- Since
- 2.3.0
- Source
wp-includes/deprecated.php:2646
Description
If the context is 'raw', then the user object or array will get minimal sanitization of the int fields.
Compatibility
Deprecated in WordPress 3.3.0. Kept for back-compatibility; avoid it in new code.
- 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
$userobject|array- The user object or array.
$contextstringoptional- How to sanitize user fields. Default 'display'.Default:
'display'
Return value
object|array- The now sanitized user object or array (will be the same type as $user).
Performance profile
How much work a call to sanitize_user_object() 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
- Light
- Scaling
- Scales with input
- Instructions
- 15–27
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below sanitize_user_object()
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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_user_object() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_object($user) && $user instanceof | 15–17 | _deprecated_function() |
!is_object($user) | 18–21 | _deprecated_function(), array_keys() |
is_object($user) && !($user instanceof) | 24–27 | _deprecated_function(), get_object_vars(), array_keys() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 72 | 15–27 | 10 | |
| 8.5 | 72 | 15–27 | 10 | |
| 8.4 | 72 | 15–27 | 10 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 74 | 15–27 | 10 | |
| 8.2 | 74 | 15–27 | 10 | |
| 8.1 | 74 | 15–27 | 10 | |
| 7.4 | 74 | 15–27 | 10 |
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.
Uses · 2
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- sanitize_user_field()Sanitizes user field based on context.
Source code
function sanitize_user_object($user, $context = 'display') { _deprecated_function( __FUNCTION__, '3.3.0' ); if ( is_object($user) ) { if ( !isset($user->ID) ) $user->ID = 0; if ( ! ( $user instanceof WP_User ) ) { $vars = get_object_vars($user); foreach ( array_keys($vars) as $field ) { if ( is_string($user->$field) || is_numeric($user->$field) ) $user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context); } } $user->filter = $context; } else { if ( !isset($user['ID']) ) $user['ID'] = 0; foreach ( array_keys($user) as $field ) $user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context); $user['filter'] = $context; } return $user;}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 6.8.8 tag, from
src/wp-includes/deprecated.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.