wp_create_user_request( string $email_address = '', string $action_name = '', array $request_data = array(), string $status = 'pending' ): int|WP_Error
- Since
- 4.9.6, 5.7.0
- Source
wp-includes/user.php:4646
Description
Requests are stored inside a post type named user_request since they can apply to both users on the site, or guests without a user account.
Compatibility
- WordPress
- since 5.7.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
$email_addressstringoptional- User email address. This can be the address of a registered or non-registered user.Default:
'' $action_namestringoptional- Name of the action that is being confirmed. Required.Default:
'' $request_dataarrayoptional- Misc data you want to send with the verification request and pass to the actions once the request is confirmed.Default:
array() $statusstringoptional- Optional request status (pending or confirmed). Default 'pending'.Default:
'pending'
Return value
int|WP_Error- Returns the request ID if successful, or a WP_Error object on failure.
Performance profile
How much work a call to wp_create_user_request() 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
- Heavy
- Scaling
- Constant
- Instructions
- 24–74
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_user_by().
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 107.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_user_by()called directly - hookthird-party callbacks
apply_filters()one call below wp_create_user_request() - optionoption read or write
get_option()one call below wp_create_user_request()
Further down the call graph this can also reach cache, serialize 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_create_user_request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_email() | 24 | sanitize_email(), sanitize_key(), is_email(), __() |
is_email() | 29–31 | sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), __() |
is_email() && $action_name && $status && $requests_query | 51 | sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), __(), user_request() |
is_email() && $action_name && $status && $requests_query | 55–56 | sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), is_wp_error(), __(), user_request() |
is_email() && $action_name && $status && !$requests_query | 69 | sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), wp_json_encode(), current_time(), current_time(), post_author() |
is_email() && $action_name && $status && !$requests_query | 73–74 | sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), is_wp_error(), wp_json_encode(), current_time(), current_time(), post_author() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 107 | 24–74 | 6 | |
| 8.5 | 107 | 24–74 | 6 | |
| 8.4 | 107 | 24–74 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 110 | 24–77 | 6 | |
| 8.2 | 110 | 24–77 | 6 | |
| 8.1 | 110 | 24–77 | 6 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 111 | 24–78 | 6 |
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 · 12
- sanitize_email()Strips out all characters that are not allowable in an email.
- sanitize_key()Sanitizes a string key.
- is_email()Verifies that an email is valid.
- __()Retrieves the translation of $text.
- _wp_privacy_action_request_types()Gets all personal data request types.
- get_user_by()Retrieves user info by a given field.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_insert_post()Inserts or update a post.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- current_time()Retrieves the current time based on specified type.
- WP_Error::__construct()Initializes the error.
- WP_Query::__construct()Constructor.
Used by · 1
- _wp_personal_data_handle_actions()Handle list table actions.
Source code
function wp_create_user_request( $email_address = '', $action_name = '', $request_data = array(), $status = 'pending' ) { $email_address = sanitize_email( $email_address ); $action_name = sanitize_key( $action_name ); if ( ! is_email( $email_address ) ) { return new WP_Error( 'invalid_email', __( 'Invalid email address.' ) ); } if ( ! in_array( $action_name, _wp_privacy_action_request_types(), true ) ) { return new WP_Error( 'invalid_action', __( 'Invalid action name.' ) ); } if ( ! in_array( $status, array( 'pending', 'confirmed' ), true ) ) { return new WP_Error( 'invalid_status', __( 'Invalid request status.' ) ); } $user = get_user_by( 'email', $email_address ); $user_id = $user && ! is_wp_error( $user ) ? $user->ID : 0; // Check for duplicates. $requests_query = new WP_Query( array( 'post_type' => 'user_request', 'post_name__in' => array( $action_name ), // Action name stored in post_name column. 'title' => $email_address, // Email address stored in post_title column. 'post_status' => array( 'request-pending', 'request-confirmed', ), 'fields' => 'ids', ) ); if ( $requests_query->found_posts ) { return new WP_Error( 'duplicate_request', __( 'An incomplete personal data request for this email address already exists.' ) ); } $request_id = wp_insert_post( array( 'post_author' => $user_id, 'post_name' => $action_name, 'post_title' => $email_address, 'post_content' => wp_json_encode( $request_data ), 'post_status' => 'request-' . $status, 'post_type' => 'user_request', 'post_date' => current_time( 'mysql', false ), 'post_date_gmt' => current_time( 'mysql', true ), ), true ); return $request_id;}Changelog
Introduced in 4.9.6. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$status parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.