wppaste
WordPress

rest_authorization_required_code(): int

Since
4.7.0
Source
wp-includes/rest-api.php:1426

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.

Returns a contextual HTTP error code for authorization failure.

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?

The function only checks is_user_logged_in(). Any request that carries a valid auth cookie, nonce, or application password counts as logged in, so a permission_callback that denies access to that user will get 403 no matter what capability is missing.

How do I actually trigger the 401 branch to test it?

401 only comes back when is_user_logged_in() is false for the current request, which means no valid auth cookie, nonce, or application password was sent at all.

Can I use this outside a REST permission_callback?

Nothing in the source ties it to the REST API classes, it is just is_user_logged_in() mapped to two integers, so it's fine to reuse anywhere you want that mapping. It does not set any HTTP header or build a WP_Error by itself, you still have to do that yourself.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
4

Executed per call on PHP 8.5. The body compiles to 5.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

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.

WhenInstructionsCalls it makes
always4is_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

Used by · 50

Show all 50

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.

  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.

About this page

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