wppaste
WordPress

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:4745
Creates and logs a user request to perform a specific action.

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

Reaches the database via get_user_by().

Scaling
Constant

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

Instructions
24–74

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • querycontent queryget_user_by()called directly
  • hookthird-party callbacksapply_filters()one call below wp_create_user_request()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
!is_email()24sanitize_email(), sanitize_key(), is_email(), __()
is_email()29–31sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), __()
is_email() && $action_name && $status && $requests_query51sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), __(), user_request()
is_email() && $action_name && $status && $requests_query55–56sanitize_email(), sanitize_key(), is_email(), _wp_privacy_action_request_types(), get_user_by(), is_wp_error(), __(), user_request()
is_email() && $action_name && $status && !$requests_query69sanitize_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_query73–74sanitize_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

PHPCompiledExecutedBranchesNotes
8.6-dev10724–746
8.510724–746
8.410724–7463 fewer instructions than PHP 8.3
8.311024–776
8.211024–776
8.111024–7761 fewer instruction than PHP 7.4
7.411124–786

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

Used by · 1

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.

  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.

5.7.0
Added the $status parameter.from the docblock
4.9.6
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.