auth_redirect()
- Since
- 1.5.0
- Source
wp-includes/pluggable.php:1195
Description
When this code is called from a page, it checks to see if the user viewing the page is logged in.
If the user is not logged in, they are redirected to the login page. The user is redirected in such a way that, upon logging in, they will be sent directly to the page they were originally trying to access.
Compatibility
- WordPress
- since 1.5.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.
Performance profile
How much work a call to auth_redirect() 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
- 27–79
- Plugin surface
- 3 hooks
- 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 136.
Third-party callbacks on 'secure_auth_redirect', 'auth_redirect_scheme', 'auth_redirect' 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
apply_filters()called directly - querycontent query
get_user_by()one call below auth_redirect()
Further down the call graph this can also reach option, 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 · 56 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost auth_redirect() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_ssl() | 27 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() | 30–34 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action() |
!is_ssl() | 30 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() | 32–36 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() | 33–37 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() | 35–43 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() | 35–39 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() | 38–46 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
is_ssl() && wp_get_referer() | 45 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
is_ssl() | 48 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
is_ssl() && wp_get_referer() | 48–52 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && wp_get_referer() | 48 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
44 further outcomes, up to 79 instructions
| always | 49 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() && get_user_option() | 51 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
| always | 51 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() && !wp_get_referer() | 51 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
is_ssl() | 51–55 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() | 51 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && wp_get_referer() | 51–55 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
is_ssl() && get_user_option() | 52 | is_ssl(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
!is_ssl() | 52 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() && get_user_option() | 54–58 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
| always | 54–58 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() && get_user_option() | 54 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
!is_ssl() | 54 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action() |
is_ssl() && !wp_get_referer() | 54–58 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && !wp_get_referer() | 54 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() | 54–58 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
is_ssl() && get_user_option() | 55–59 | is_ssl(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 55 | is_ssl(), force_ssl_admin(), apply_filters(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
| always | 56–60 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() && get_user_option() | 57–61 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
!is_ssl() | 57–61 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
!is_ssl() && !wp_get_referer() | 57–61 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 58–62 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
!is_ssl() | 59–63 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option() |
wp_get_referer() | 67 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
wp_get_referer() | 69 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
| always | 70 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && wp_get_referer() | 70 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
| always | 72 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && wp_get_referer() | 72 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), wp_get_referer(), wp_login_url(), wp_redirect(), exit() |
get_user_option() | 73 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
!wp_get_referer() | 73 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() | 73 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
get_user_option() | 74 | is_ssl(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
get_user_option() | 75 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
!wp_get_referer() | 75 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() | 75 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 76 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
get_user_option() | 76 | is_ssl(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
!is_ssl() && !wp_get_referer() | 76 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 77 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 78 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), wp_redirect(), exit() |
!is_ssl() && !wp_get_referer() | 78 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), nocache_headers(), wp_get_referer(), set_url_scheme(), wp_login_url(), wp_redirect(), exit() |
!is_ssl() && get_user_option() | 79 | is_ssl(), force_ssl_admin(), apply_filters(), is_ssl(), set_url_scheme(), wp_redirect(), exit(), apply_filters(), wp_validate_auth_cookie(), do_action(), get_user_option(), set_url_scheme(), wp_redirect(), exit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 136 | 27–79 | 12 | |
| 8.5 | 136 | 27–79 | 12 | |
| 8.4 | 136 | 27–79 | 12 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 143 | 27–69 | 12 | |
| 8.2 | 143 | 27–69 | 12 | |
| 8.1 | 143 | 27–69 | 12 | 5 fewer instructions than PHP 7.4 |
| 7.4 | 148 | 27–72 | 12 |
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 · 3
3 hooks fire while auth_redirect() runs, in this order:
- apply_filters( secure_auth_redirect )filterline 1205 (+10 into the body)
Filters whether to use a secure authentication redirect.
- apply_filters( auth_redirect_scheme )filterline 1225 (+30 into the body)
Filters the authentication redirect scheme.
- do_action( auth_redirect )actionline 1236 (+41 into the body)
Fires before the authentication redirect.
Uses · 13
- is_ssl()Determines if SSL is used.
- force_ssl_admin()Determines whether to force SSL used for the Administration Screens.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_redirect()Redirects to another page.
- set_url_scheme()Sets the scheme for a URL.
- wp_validate_auth_cookie()Validates authentication cookie.
- do_action()Calls the callback functions that have been added to an action hook.
- get_user_option()Retrieves user option that can be either per Site or per Network.
- nocache_headers()Sets the HTTP headers to prevent caching for the different browsers.
- wp_get_referer()Retrieves referer from '_wp_http_referer' or HTTP referer.
Show all 13
- wp_login_url()Retrieves the login URL.
Used by · 1
- WP_Customize_Manager::setup_theme()Starts preview and customize theme.
Source code
function auth_redirect() { $secure = ( is_ssl() || force_ssl_admin() ); /** * Filters whether to use a secure authentication redirect. * * @since 3.1.0 * * @param bool $secure Whether to use a secure authentication redirect. Default false. */ $secure = apply_filters( 'secure_auth_redirect', $secure ); // If https is required and request is http, redirect. if ( $secure && ! is_ssl() && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) { wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); exit; } else { wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); exit; } } /** * Filters the authentication redirect scheme. * * @since 2.9.0 * * @param string $scheme Authentication redirect scheme. Default empty. */ $scheme = apply_filters( 'auth_redirect_scheme', '' ); $user_id = wp_validate_auth_cookie( '', $scheme ); if ( $user_id ) { /** * Fires before the authentication redirect. * * @since 2.8.0 * * @param int $user_id User ID. */ do_action( 'auth_redirect', $user_id ); // If the user wants ssl but the session is not ssl, redirect. if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) { if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) { wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) ); exit; } else { wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); exit; } } return; // The cookie is good, so we're done. } // The cookie is no good, so force login. nocache_headers(); if ( str_contains( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) { $redirect = wp_get_referer(); } else { $redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); } $login_url = wp_login_url( $redirect, true ); wp_redirect( $login_url ); exit; }Changelog
Introduced in 1.5.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.8.8 tag, from
src/wp-includes/pluggable.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.