wp_redirectfilter-hookWP 2.1.0

Allows you to change the redirect address passed to the wp_redirect() function.

Usage

add_filter( 'wp_redirect', 'wp_kama_redirect_filter', 10, 2 );

/**
 * Function for `wp_redirect` filter-hook.
 * 
 * @param string $location The path or URL to redirect to.
 * @param int    $status   The HTTP response status code to use.
 *
 * @return string
 */
function wp_kama_redirect_filter( $location, $status ){
	// filter...
	return $location;
}
$location(string)
The path or URL to redirect to.
$status(integer)
The redirect status code (HTTP status code).

Examples

#1 Track where a redirect occurs

Sometimes, during debugging, it can be difficult to track (catch, find) which section of code creates redirects in WordPress (it’s hard to catch the redirect). But if such redirects are made by WordPress functions: wp_redirect() or wp_safe_redirect() - there is a solution!

Below is shown how to easily catch redirects.

With debug enabled, use the code (preferably somewhere earlier, e.g. in mu-plugins):

Variant via closure:

add_filter( 'wp_redirect', function( $location ) {
	//error_log( print_r( debug_backtrace( 1 ), true ) );
	//error_log( print_r( debug_backtrace( 0 ), true ) );
	error_log( print_r( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), true ) );
	return $location;
} );

Variant via function:

add_filter( 'wp_redirect', 'wp_redirect_print_debug_backtrace' );
function wp_redirect_print_debug_backtrace( $location ) {

	error_log( print_r( debug_backtrace( true ), true ) );

	// or
	/*
	ob_start();
	debug_print_backtrace();
	$log = ob_get_clean();
	error_log( $log );
	*/

	return $location;
}

Check the capabilities of the debug_backtrace function to change the completeness of the data provided.

You can also use the debug_print_backtrace() function, but it immediately outputs the data to the screen, so you need to “buffer” it in order to pass it to error_log().

Now let’s try, for example, visiting the example.com/login/ page, from which the engine will redirect us to the example.com/wp-login.php page. In the log we will get the following information:

Array
(
	[0] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\class-wp-hook.php
			[line] => 289
			[function] => wp_redirect_print_debug_backtrace
			[args] => Array
				(
					[0] => https://wp-test.ru/wp-login.php
				)

		)

	[1] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\plugin.php
			[line] => 206
			[function] => apply_filters
			[class] => WP_Hook
			[type] => ->
			[args] => Array
				(
					[0] => https://wp-test.ru/wp-login.php
					[1] => Array
						(
							[0] => https://wp-test.ru/wp-login.php
							[1] => 302
						)

				)

		)

	[2] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\pluggable.php
			[line] => 1257
			[function] => apply_filters
			[args] => Array
				(
					[0] => wp_redirect
					[1] => https://wp-test.ru/wp-login.php
					[2] => 302
				)

		)

	[3] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\canonical.php
			[line] => 971
			[function] => wp_redirect
			[args] => Array
				(
					[0] => https://wp-test.ru/wp-login.php
				)

		)

	[4] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\class-wp-hook.php
			[line] => 287
			[function] => wp_redirect_admin_locations
			[args] => Array
				(
					[0] =>
				)

		)

	[5] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\class-wp-hook.php
			[line] => 311
			[function] => apply_filters
			[class] => WP_Hook
			[type] => ->
			[args] => Array
				(
					[0] =>
					[1] => Array
						(
							[0] =>
						)

				)

		)

	[6] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\plugin.php
			[line] => 478
			[function] => do_action
			[class] => WP_Hook
			[type] => ->
			[args] => Array
				(
					[0] => Array
						(
							[0] =>
						)

				)

		)

	[7] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-includes\template-loader.php
			[line] => 13
			[function] => do_action
			[args] => Array
				(
					[0] => template_redirect
				)

		)

	[8] => Array
		(
			[file] => F:\server\www\wp-test.ru\wp-blog-header.php
			[line] => 19
			[args] => Array
				(
					[0] => F:\server\www\wp-test.ru\wp-includes\template-loader.php
				)

			[function] => require_once
		)

	[9] => Array
		(
			[file] => F:\server\www\wp-test.ru\index.php
			[line] => 17
			[args] => Array
				(
					[0] => F:\server\www\wp-test.ru\wp-blog-header.php
				)

			[function] => require
		)

)

Thanks to this debugging information, we determined that the redirect is caused by the following action hook:

add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );

It is attached in the default-filters.php file and runs the wp_redirect_admin_locations() function, which creates the redirect itself.

Changelog

Since 2.1.0 Introduced.

Where the hook is called

wp_redirect()
wp_redirect
wp-includes/pluggable.php 1500
$location = apply_filters( 'wp_redirect', $location, $status );

Where the hook is used in WordPress

wp-includes/class-wp-customize-manager.php 1943
add_filter( 'wp_redirect', array( $this, 'add_state_query_params' ) );