rest_authorization_required_code(): int
- Since
- 4.7.0
- Source
wp-includes/rest-api.php:1430
Maps the current user's login state to the HTTP status code a REST permission check should return: 401 for logged-out requests, 403 for logged-in ones. It relies entirely on is_user_logged_in(), so it never inspects capabilities, it only tells you whether someone is authenticated at all. Core's built-in REST controllers (application passwords, attachments, autosaves, and more) call it inside their permission_callback methods to build the status for a WP_Error.
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.
Return value
int- 401 if the user is not logged in, 403 if the user is logged in.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Check what status code the function returns for a logged-in vs. logged-out user
Print the return value while the sandbox admin is authenticated, then again after switching to user ID 0.
$logged_in_status = rest_authorization_required_code();
echo esc_html( 'While logged in: ' . $logged_in_status );
$original_user_id = get_current_user_id();
wp_set_current_user( 0 );
$logged_out_status = rest_authorization_required_code();
echo esc_html( ' | While logged out: ' . $logged_out_status );
wp_set_current_user( $original_user_id );
echo esc_html( ' | Restored user ID: ' . get_current_user_id() );wp_set_current_user( 0 ) only changes the in-memory current user for this request, it does not log the browser session out.
Restrict a custom REST route to users who can manage_options
Register a route whose permission_callback uses the function to build a WP_Error status, then dispatch a request to it internally as both the admin and an anonymous user.
$request = new WP_REST_Request( 'GET', '/wppaste/v1/secrets' );
$admin_response = rest_do_request( $request );
if ( $admin_response->is_error() ) {
$error = $admin_response->as_error();
echo esc_html( 'Admin denied, status ' . $error->get_error_data()['status'] );
} else {
echo esc_html( 'Admin allowed, secret is ' . $admin_response->get_data()['secret'] );
}
$original_user_id = get_current_user_id();
wp_set_current_user( 0 );
$anon_response = rest_do_request( $request );
$anon_error = $anon_response->as_error();
echo esc_html( ' | Anonymous denied, status ' . $anon_error->get_error_data()['status'] );
wp_set_current_user( $original_user_id );Common problems and fixes · 3
- Why does my REST endpoint always return 403 and never 401, even when the request has no permission?
- How do I actually trigger the 401 branch to test it?
- Can I use this outside a REST permission_callback?
Why does my REST endpoint always return 403 and never 401, even when the request has no permission?
How do I actually trigger the 401 branch to test it?
Can I use this outside a REST permission_callback?
Alternatives and related functions
current_user_can- When the real question is whether the user has a specific capability, not just whether any user is logged in.
is_user_logged_in- When you only need the boolean login state and don't need an HTTP status code out of it.
status_header- When you need to send an HTTP status code directly for a non-REST request, such as a template or admin-post.php handler.
Performance profile
How much work a call to rest_authorization_required_code() 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
- Trivial
- Scaling
- Constant
- Instructions
- 4
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 5.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_authorization_required_code() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4 | is_user_logged_in() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 5 instructions, 4 executed per call, 1 branch. The work does not change between versions.
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 · 1
- is_user_logged_in()Determines whether the current visitor is a logged in user.
Used by · 50
- WP_REST_Abilities_V1_Run_Controller::check_ability_permissions()Checks if a given request has permission to execute a specific ability.
- WP_REST_Application_Passwords_Controller::create_item_permissions_check()Checks if a given request has access to create application passwords.
- WP_REST_Application_Passwords_Controller::delete_item_permissions_check()Checks if a given request has access to delete a specific application password for a user.
- WP_REST_Application_Passwords_Controller::delete_items_permissions_check()Checks if a given request has access to delete all application passwords for a user.
- WP_REST_Application_Passwords_Controller::do_permissions_check()Performs a permissions check for the request.
- WP_REST_Application_Passwords_Controller::get_current_item_permissions_check()Checks if a given request has access to get the currently used application password for a user.
- WP_REST_Application_Passwords_Controller::get_item_permissions_check()Checks if a given request has access to get a specific application password.
- WP_REST_Application_Passwords_Controller::get_items_permissions_check()Checks if a given request has access to get application passwords.
- WP_REST_Application_Passwords_Controller::update_item_permissions_check()Checks if a given request has access to update application passwords.
- WP_REST_Attachments_Controller::create_item_permissions_check()Checks if a given request has access to create an attachment.
- WP_REST_Attachments_Controller::edit_media_item_permissions_check()Checks if a given request has access to editing media.
- WP_REST_Autosaves_Controller::get_items_permissions_check()Checks if a given request has access to get autosaves.
Show all 50
- WP_REST_Block_Directory_Controller::get_items_permissions_check()Checks whether a given request has permission to install and activate plugins.
- WP_REST_Block_Pattern_Categories_Controller::get_items_permissions_check()Checks whether a given request has permission to read block patterns.
- WP_REST_Block_Patterns_Controller::get_items_permissions_check()Checks whether a given request has permission to read block patterns.
- WP_REST_Block_Renderer_Controller::get_item_permissions_check()Checks if a given request has access to read blocks.
- WP_REST_Block_Types_Controller::check_read_permission()Checks whether a given block type should be visible.
- WP_REST_Comments_Controller::create_item_permissions_check()Checks if a given request has access to create a comment.
- WP_REST_Comments_Controller::delete_item_permissions_check()Checks if a given request has access to delete a comment.
- WP_REST_Comments_Controller::get_item_permissions_check()Checks if a given request has access to read the comment.
- WP_REST_Comments_Controller::get_items_permissions_check()Checks if a given request has access to read comments.
- WP_REST_Comments_Controller::update_item_permissions_check()Checks if a given REST request has access to update a comment.
- WP_REST_Edit_Site_Export_Controller::permissions_check()Checks whether a given request has permission to export.
- WP_REST_Font_Collections_Controller::get_items_permissions_check()Checks whether the user has permissions to use the Fonts Collections.
- WP_REST_Font_Faces_Controller::get_item_permissions_check()Checks if a given request has access to a font face.
- WP_REST_Font_Faces_Controller::get_items_permissions_check()Checks if a given request has access to font faces.
- WP_REST_Font_Families_Controller::get_item_permissions_check()Checks if a given request has access to a font family.
- WP_REST_Font_Families_Controller::get_items_permissions_check()Checks if a given request has access to font families.
- WP_REST_Global_Styles_Controller::get_item_permissions_check()Checks if a given request has access to read a single global style.
- WP_REST_Global_Styles_Controller::get_theme_item_permissions_check()Checks if a given request has access to read a single theme global styles config.
- WP_REST_Global_Styles_Controller::update_item_permissions_check()Checks if a given request has access to write a single global styles config.
- WP_REST_Icons_Controller::get_items_permissions_check()Checks whether a given request has permission to read icons.
- WP_REST_Menu_Items_Controller::check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
- WP_REST_Menu_Locations_Controller::check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
- WP_REST_Menus_Controller::check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
- WP_REST_Meta_Fields::delete_meta_value()Deletes a meta value for an object.
- WP_REST_Meta_Fields::update_meta_value()Updates a meta value for an object.
- WP_REST_Meta_Fields::update_multi_meta_value()Updates multiple meta values for an object.
- WP_REST_Navigation_Fallback_Controller::get_item_permissions_check()Checks if a given request has access to read fallbacks.
- WP_REST_Pattern_Directory_Controller::get_items_permissions_check()Checks whether a given request has permission to view the local block pattern directory.
- WP_REST_Plugins_Controller::check_read_permission()Checks if the given plugin can be viewed by the current user.
- WP_REST_Plugins_Controller::create_item_permissions_check()Checks if a given request has access to upload plugins.
- WP_REST_Plugins_Controller::delete_item_permissions_check()Checks if a given request has access to delete a specific plugin.
- WP_REST_Plugins_Controller::get_item_permissions_check()Checks if a given request has access to get a specific plugin.
- WP_REST_Plugins_Controller::get_items_permissions_check()Checks if a given request has access to get plugins.
- WP_REST_Plugins_Controller::plugin_status_permission_check()Handle updating a plugin's status.
- WP_REST_Plugins_Controller::update_item_permissions_check()Checks if a given request has access to update a specific plugin.
- WP_REST_Post_Statuses_Controller::get_item_permissions_check()Checks if a given request has access to read a post status.
- WP_REST_Post_Statuses_Controller::get_items_permissions_check()Checks whether a given request has permission to read post statuses.
- WP_REST_Post_Types_Controller::get_item()Retrieves a specific post type.
Source code
function rest_authorization_required_code() { return is_user_logged_in() ? 403 : 401;}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 7.0.4 tag, from
src/wp-includes/rest-api.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.