wppaste
WordPress

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:1481
Redirects to another page.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
18–55

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.

Plugin surface
3 hooks

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.

Called by
21

21 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always18apply_filters(), apply_filters()
!$status && !is_string($x_redirect_by)42apply_filters(), apply_filters(), wp_sanitize_redirect(), apply_filters(), header()
!$status && !is_string($x_redirect_by)45apply_filters(), apply_filters(), wp_sanitize_redirect(), status_header(), apply_filters(), header()
!$status && is_string($x_redirect_by)46apply_filters(), apply_filters(), wp_sanitize_redirect(), apply_filters(), header(), header()
$status && !is_string($x_redirect_by)46–48apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), apply_filters(), header()
!$status && is_string($x_redirect_by)49apply_filters(), apply_filters(), wp_sanitize_redirect(), status_header(), apply_filters(), header(), header()
$status && !is_string($x_redirect_by)49–51apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), status_header(), apply_filters(), header()
$status && is_string($x_redirect_by)50–52apply_filters(), apply_filters(), __(), wp_die(), wp_sanitize_redirect(), apply_filters(), header(), header()
$status && is_string($x_redirect_by)53–55apply_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:

  1. apply_filters( wp_redirect )filterline 1492 (+11 into the body)

    Filters the redirect location.

  2. apply_filters( wp_redirect_status )filterline 1502 (+21 into the body)

    Filters the redirect HTTP response status code to use.

  3. apply_filters( x_redirect_by )filterline 1529 (+48 into the body)

    Filters the X-Redirect-By header.

Uses · 5

Used by · 21

Show all 21

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.

  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.

5.4.0
On invalid status codes, wp_die() is called.from the docblock
5.1.0
The $x_redirect_by parameter was added.from the docblock
1.5.1
Introduced.from the docblock

About this page

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