WP_REST_Users_Controller::delete_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php:887
Compatibility
- WordPress
- since 4.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
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Users_Controller::delete_item() 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
- 13–76
- Plugin surface
- 1 hook
- 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 120.
Third-party callbacks on 'rest_delete_user' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - querycontent query
get_user_by()one call below WP_REST_Users_Controller::delete_item()
Further down the call graph this can also reach cache, option, 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 · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Users_Controller::delete_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_multisite() | 13 | is_multisite(), __() |
!is_multisite() && is_wp_error() | 15 | is_multisite(), ->get_user(), is_wp_error() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign === false | 40–42 | is_multisite(), ->get_user(), is_wp_error(), __() |
!is_multisite() && !is_wp_error() | 40–42 | is_multisite(), ->get_user(), is_wp_error(), __(), sprintf() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign === false | 44–46 | is_multisite(), ->get_user(), is_wp_error(), absint(), __() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && !get_userdata() | 44–46 | is_multisite(), ->get_user(), is_wp_error(), get_userdata(), __() |
!is_multisite() && !is_wp_error() | 44–46 | is_multisite(), ->get_user(), is_wp_error(), absint(), __(), sprintf() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && !get_userdata() | 48–50 | is_multisite(), ->get_user(), is_wp_error(), absint(), get_userdata(), __() |
!is_multisite() && !is_wp_error() && empty($reassign) | 56–58 | is_multisite(), ->get_user(), is_wp_error(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), __() |
!is_multisite() && !is_wp_error() && empty($reassign) | 60–62 | is_multisite(), ->get_user(), is_wp_error(), absint(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), __() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && get_userdata() | 62–64 | is_multisite(), ->get_user(), is_wp_error(), get_userdata(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), __() |
!is_multisite() && !is_wp_error() && empty($reassign) | 64–66 | is_multisite(), ->get_user(), is_wp_error(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), ->get_data(), deleted(), do_action() |
4 further outcomes, up to 76 instructions
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && get_userdata() | 66–68 | is_multisite(), ->get_user(), is_wp_error(), absint(), get_userdata(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), __() |
!is_multisite() && !is_wp_error() && empty($reassign) | 68–70 | is_multisite(), ->get_user(), is_wp_error(), absint(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), ->get_data(), deleted(), do_action() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && get_userdata() | 70–72 | is_multisite(), ->get_user(), is_wp_error(), get_userdata(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), ->get_data(), deleted(), do_action() |
!is_multisite() && !is_wp_error() && !empty($reassign) && $reassign !== false && get_userdata() | 74–76 | is_multisite(), ->get_user(), is_wp_error(), absint(), get_userdata(), ->set_param(), ->prepare_item_for_response(), wp_delete_user(), ->get_data(), deleted(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 119 | 13–75 | 9 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 120 | 13–76 | 9 | |
| 8.4 | 120 | 13–76 | 9 | |
| 8.3 | 120 | 13–76 | 9 | |
| 8.2 | 120 | 13–76 | 9 | |
| 8.1 | 120 | 13–76 | 9 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 121 | 13–77 | 9 |
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 · 1
One hook fires while WP_REST_Users_Controller::delete_item() runs, in this order:
- do_action( rest_delete_user )actionline 961 (+74 into the body)
Fires immediately after a user is deleted via the REST API.
Uses · 11
- is_multisite()Determines whether Multisite is enabled.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- absint()Converts a value to non-negative integer.
- get_userdata()Retrieves user info by user ID.
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
- WP_REST_Users_Controller::get_user()Get the user, if the ID is valid.
- WP_REST_Users_Controller::prepare_item_for_response()Prepares a single user output for response.
- WP_REST_Response::__construct()
Used by · 1
- WP_REST_Users_Controller::delete_current_item()Deletes the current user.
Source code
public function delete_item( $request ) { // We don't support delete requests in multisite. if ( is_multisite() ) { return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 501 ) ); } $user = $this->get_user( $request['id'] ); if ( is_wp_error( $user ) ) { return $user; } $id = $user->ID; $reassign = false === $request['reassign'] ? null : absint( $request['reassign'] ); $force = isset( $request['force'] ) ? (bool) $request['force'] : false; // We don't support trashing for users. if ( ! $force ) { return new WP_Error( 'rest_trash_not_supported', /* translators: %s: force=true */ sprintf( __( "Users do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) ); } if ( ! empty( $reassign ) ) { if ( $reassign === $id || ! get_userdata( $reassign ) ) { return new WP_Error( 'rest_user_invalid_reassign', __( 'Invalid user ID for reassignment.' ), array( 'status' => 400 ) ); } } $request->set_param( 'context', 'edit' ); $previous = $this->prepare_item_for_response( $user, $request ); // Include user admin functions to get access to wp_delete_user(). require_once ABSPATH . 'wp-admin/includes/user.php'; $result = wp_delete_user( $id, $reassign ); if ( ! $result ) { return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 500 ) ); } $response = new WP_REST_Response(); $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data(), ) ); /** * Fires immediately after a user is deleted via the REST API. * * @since 4.7.0 * * @param WP_User $user The user data. * @param WP_REST_Response $response The response returned from the API. * @param WP_REST_Request $request The request sent to the API. */ do_action( 'rest_delete_user', $user, $response, $request ); return $response; }Changelog
Introduced in 4.7.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.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.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.