On this page
wp_redirect( string $location, int $status = 302, string $x_redirect_by = 'WordPress' ): bool
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_location’ filters:
if ( wp_redirect( $url ) ) {
exit;
}
Parameters
$locationstring Required-
The path or URL to redirect to.
$statusint Optional-
HTTP response status code to use. Default
'302'(Moved Temporarily).Default:
302 $x_redirect_bystring Optional-
The application doing the redirect. Default
'WordPress'.Default:
'WordPress'
Return
bool False if the redirect was cancelled, true otherwise.
Source
File: wp-includes/pluggable.php. View all references
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 $x_redirect_by The application doing the redirect.
* @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;
}
Hooks
- apply_filters( 'wp_redirect',
string $location ,int $status ) -
Filters the redirect location.
- apply_filters( 'wp_redirect_status',
int $status ,string $location ) -
Filters the redirect HTTP response status code to use.
- apply_filters( 'x_redirect_by',
string $x_redirect_by ,int $status ,string $location ) -
Filters the X-Redirect-By header.
Related
Uses
| Uses | Description |
|---|---|
| wp_sanitize_redirect() wp-includes/pluggable.php | Sanitizes a URL for use in a redirect. |
| status_header() wp-includes/functions.php | Sets HTTP status header. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_die() wp-includes/functions.php | Kills WordPress execution and displays HTML page with an error message. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| do_favicon() wp-includes/functions.php | Displays the favicon.ico file content. |
| WP_Recovery_Mode_Link_Service::handle_begin_link() wp-includes/class-wp-recovery-mode-link-service.php | Enters recovery mode when the user hits wp-login.php with a valid recovery mode link. |
| resume_theme() wp-admin/includes/theme.php | Tries to resume a single theme. |
| resume_plugin() wp-admin/includes/plugin.php | Tries to resume a single plugin. |
| wp_media_attach_action() wp-admin/includes/media.php | Encapsulates the logic for Attach/Detach actions. |
| WP_List_Table::set_pagination_args() wp-admin/includes/class-wp-list-table.php | An internal method that sets all the necessary pagination arguments |
| wp_dashboard_setup() wp-admin/includes/dashboard.php | Registers dashboard widgets. |
| activate_plugin() wp-admin/includes/plugin.php | Attempts activation of plugin in a “sandbox” and redirects on success. |
| redirect_post() wp-admin/includes/post.php | Redirects to previous page. |
| do_dismiss_core_update() wp-admin/update-core.php | Dismiss a core update. |
| do_undismiss_core_update() wp-admin/update-core.php | Undismiss a core update. |
| WP_Customize_Manager::after_setup_theme() wp-includes/class-wp-customize-manager.php | Callback to validate a theme once it is loaded |
| spawn_cron() wp-includes/cron.php | Sends a request to run cron through HTTP request that doesn’t halt page loading. |
| wp_safe_redirect() wp-includes/pluggable.php | Performs a safe (local) redirect, using wp_redirect() . |
| auth_redirect() wp-includes/pluggable.php | Checks if a user is logged in, if not it redirects them to the login page. |
| wp_old_slug_redirect() wp-includes/query.php | Redirect old slugs to the correct permalink. |
| wp_not_installed() wp-includes/load.php | Redirect to the installer if WordPress is not installed. |
| wp_redirect_admin_locations() wp-includes/canonical.php | Redirects a variety of shorthand URLs to the admin. |
| redirect_canonical() wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
| maybe_redirect_404() wp-includes/ms-functions.php | Corrects 404 redirects when NOBLOGREDIRECT is defined. |
| wpmu_admin_do_redirect() wp-includes/ms-deprecated.php | Redirect a user based on $_GET or $_POST arguments. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_redirect