wp_redirect( string $location, int $status = 302, string|false $x_redirect_by = 'WordPress' ): bool
- Since
- 1.5.1, 5.1.0, 5.4.0
- Source
wp-includes/pluggable.php:1385
Description
Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;:
wp_redirect( $url );
exit; Exiting can also be selectively manipulated by using wp_redirect() as a conditional in conjunction with the 'wp_redirect' and 'wp_redirect_status' filters:
if ( wp_redirect( $url ) ) {
exit;
}Compatibility
- WordPress
- since 5.4.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
$locationstring- The path or URL to redirect to.
$statusintoptional- HTTP response status code to use. Default '302' (Moved Temporarily).Default:
302 $x_redirect_bystring|falseoptional- The application doing the redirect or false to omit. Default 'WordPress'.Default:
'WordPress'
Return value
bool- False if the redirect was canceled, true otherwise.
Performance profile
How much work a call to wp_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
- Trivial
- Scaling
- Constant
- Instructions
- 18–55
- Plugin surface
- 3 hooks
- Called by
- 21
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, depending on the branch taken. The body compiles to 56.
Third-party callbacks on 'wp_redirect', 'wp_redirect_status', 'x_redirect_by' run inside this call, and their cost is not bounded by anything here.
21 places 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
Further down the call graph this can also reach query, 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 · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_redirect() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 18 | apply_filters(), apply_filters() |
!$status && !is_string($x_redirect_by) | 42 | apply_filters(), apply_filters(), wp_sanitize_redirect(), apply_filters(), header() |
!$status && !is_string($x_redirect_by) | 45 | apply_filters(), apply_filters(), wp_sanitize_redirect(), status_header(), apply_filters(), header() |
!$status && is_string($x_redirect_by) | 46 | apply_filters(), apply_filters(), wp_sanitize_redirect(), apply_filters(), header(), header() |
$status && !is_string($x_redirect_by) | 46–48 | apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), apply_filters(), header() |
!$status && is_string($x_redirect_by) | 49 | apply_filters(), apply_filters(), wp_sanitize_redirect(), status_header(), apply_filters(), header(), header() |
$status && !is_string($x_redirect_by) | 49–51 | apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), status_header(), apply_filters(), header() |
$status && is_string($x_redirect_by) | 50–52 | apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), apply_filters(), header(), header() |
$status && is_string($x_redirect_by) | 53–55 | apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), status_header(), apply_filters(), header(), header() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 56 instructions, 18–55 executed per call, 5 branches. 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.
Hooks and filters fired · 3
3 hooks fire while wp_redirect() runs, in this order:
- apply_filters( wp_redirect_status )filterline 1406 (+21 into the body)
Filters the redirect HTTP response status code to use.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- __()Retrieves the translation of $text.
- wp_sanitize_redirect()Sanitizes a URL for use in a redirect.
- status_header()Sets HTTP status header.
Used by · 21
- WP_Customize_Manager::after_setup_theme()Callback to validate a theme once it is loaded
- WP_List_Table::set_pagination_args()Sets all the necessary pagination arguments.
- WP_Recovery_Mode_Link_Service::handle_begin_link()Enters recovery mode when the user hits wp-login.php with a valid recovery mode link.
- activate_plugin()Attempts activation of plugin in a "sandbox" and redirects on success.
- auth_redirect()Checks if a user is logged in, if not it redirects them to the login page.
- do_dismiss_core_update()Dismiss a core update.
- do_favicon()Displays the favicon.ico file content.
- do_undismiss_core_update()Undismiss a core update.
- maybe_redirect_404()Corrects 404 redirects when NOBLOGREDIRECT is defined.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- redirect_post()Redirects to previous page.
- resume_plugin()Tries to resume a single plugin.
Show all 21
- resume_theme()Tries to resume a single theme.
- spawn_cron()Sends a request to run cron through HTTP request that doesn't halt page loading.
- wp_dashboard_setup()Registers dashboard widgets.
- wp_media_attach_action()Encapsulates the logic for Attach/Detach actions.
- wp_not_installed()Redirects to the installer if WordPress is not installed.
- wp_old_slug_redirect()Redirect old slugs to the correct permalink.
- wp_redirect_admin_locations()Redirects a variety of shorthand URLs to the admin.
- wp_safe_redirect()Performs a safe (local) redirect, using wp_redirect().
- wpmu_admin_do_redirect()Redirect a user based on $_GET or $_POST arguments.
Source code
function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { global $is_IIS; /** * Filters the redirect location. * * @since 2.1.0 * * @param string $location The path or URL to redirect to. * @param int $status The HTTP response status code to use. */ $location = apply_filters( 'wp_redirect', $location, $status ); /** * Filters the redirect HTTP response status code to use. * * @since 2.3.0 * * @param int $status The HTTP response status code to use. * @param string $location The path or URL to redirect to. */ $status = apply_filters( 'wp_redirect_status', $status, $location ); if ( ! $location ) { return false; } if ( $status < 300 || 399 < $status ) { wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) ); } $location = wp_sanitize_redirect( $location ); if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) { status_header( $status ); // This causes problems on IIS and some FastCGI setups. } /** * Filters the X-Redirect-By header. * * Allows applications to identify themselves when they're doing a redirect. * * @since 5.1.0 * * @param string|false $x_redirect_by The application doing the redirect or false to omit the header. * @param int $status Status code to use. * @param string $location The path to redirect to. */ $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location ); if ( is_string( $x_redirect_by ) ) { header( "X-Redirect-By: $x_redirect_by" ); } header( "Location: $location", true, $status ); return true; }Changelog
Introduced in 1.5.1. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$x_redirect_by parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.