wp_create_user_request

The timeline below displays how wordpress function wp_create_user_request has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Creates and logs a user request to perform a specific action.
 *
 * 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.
 *
 * @since 4.9.6
 * @since 5.7.0 Added the `$status` parameter.
 *
 * @param string $email_address           User email address. This can be the address of a registered
 *                                        or non-registered user.
 * @param string $action_name             Name of the action that is being confirmed. Required.
 * @param array  $request_data            Misc data you want to send with the verification request and pass
 *                                        to the actions once the request is confirmed.
 * @param string $status                  Optional request status (pending or confirmed). Default 'pending'.
 * @return int|WP_Error                   Returns the request ID if successful, or a WP_Error object on failure.
 */
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;
}

WordPress Version: 5.7

/**
 * Create and log a user request to perform a specific action.
 *
 * 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.
 *
 * @since 4.9.6
 * @since 5.7.0 Added the `$status` parameter.
 *
 * @param string $email_address           User email address. This can be the address of a registered
 *                                        or non-registered user.
 * @param string $action_name             Name of the action that is being confirmed. Required.
 * @param array  $request_data            Misc data you want to send with the verification request and pass
 *                                        to the actions once the request is confirmed.
 * @param string $status                  Optional request status (pending or confirmed). Default 'pending'.
 * @return int|WP_Error                   Returns the request ID if successful, or a WP_Error object on failure.
 */
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;
}

WordPress Version: 5.6

/**
 * Create and log a user request to perform a specific action.
 *
 * 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.
 *
 * @since 4.9.6
 *
 * @param string $email_address User email address. This can be the address of a registered or non-registered user.
 * @param string $action_name   Name of the action that is being confirmed. Required.
 * @param array  $request_data  Misc data you want to send with the verification request and pass to the actions once the request is confirmed.
 * @return int|WP_Error Returns the request ID if successful, or a WP_Error object on failure.
 */
function wp_create_user_request($email_address = '', $action_name = '', $request_data = array())
{
    $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.'));
    }
    $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 user privacy 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-pending', 'post_type' => 'user_request', 'post_date' => current_time('mysql', false), 'post_date_gmt' => current_time('mysql', true)), true);
    return $request_id;
}

WordPress Version: 5.2

/**
 * Create and log a user request to perform a specific action.
 *
 * 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.
 *
 * @since 4.9.6
 *
 * @param string $email_address User email address. This can be the address of a registered or non-registered user.
 * @param string $action_name   Name of the action that is being confirmed. Required.
 * @param array  $request_data  Misc data you want to send with the verification request and pass to the actions once the request is confirmed.
 * @return int|WP_Error Returns the request ID if successful, or a WP_Error object on failure.
 */
function wp_create_user_request($email_address = '', $action_name = '', $request_data = array())
{
    $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 (!$action_name) {
        return new WP_Error('invalid_action', __('Invalid action name.'));
    }
    $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 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-pending', 'post_type' => 'user_request', 'post_date' => current_time('mysql', false), 'post_date_gmt' => current_time('mysql', true)), true);
    return $request_id;
}

WordPress Version: .10

/**
 * Create and log a user request to perform a specific action.
 *
 * 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.
 *
 * @since 4.9.6
 *
 * @param string $email_address User email address. This can be the address of a registered or non-registered user.
 * @param string $action_name   Name of the action that is being confirmed. Required.
 * @param array  $request_data  Misc data you want to send with the verification request and pass to the actions once the request is confirmed.
 * @return int|WP_Error Returns the request ID if successful, or a WP_Error object on failure.
 */
function wp_create_user_request($email_address = '', $action_name = '', $request_data = array())
{
    $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 (!$action_name) {
        return new WP_Error('invalid_action', __('Invalid action name.'));
    }
    $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' => 'any',
        'fields' => 'ids',
    ));
    if ($requests_query->found_posts) {
        return new WP_Error('duplicate_request', __('A 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-pending', 'post_type' => 'user_request', 'post_date' => current_time('mysql', false), 'post_date_gmt' => current_time('mysql', true)), true);
    return $request_id;
}