apply_filters( ‘wp_redirect’, string $location, int $status )

Filters the redirect location.

Parameters

$locationstring
The path or URL to redirect to.
$statusint
The HTTP response status code to use.

Source

$location = apply_filters( 'wp_redirect', $location, $status );

Changelog

VersionDescription
2.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Overview of wp_redirect Hook
    The apply_filters( 'wp_redirect', string $location, int $status ) hook in WordPress allows developers to modify the redirect URL ($location) and the HTTP status code ($status) before WordPress executes a redirection. This can be useful for modifying or validating the destination URL or for changing the response code to suit specific needs.

    add_filter( 'wp_redirect', 'wpdocs_modify_redirect_status', 10, 2 );
    
    function wpdocs_modify_redirect_status( $location, $status ) {
        if ( strpos( $location, 'permanent' ) !== false ) {
            $status = 301; // Permanent redirect
        }
        return apply_filters( 'wp_redirect', $location, $status );
    }

    In this example, if the redirect URL contains “permanent,” the status code changes to 301.

You must log in before being able to contribute a note or feedback.