wp_redirect

Home » Functions » wp_redirect

WordPress provides a handy function called "wp_redirect" that allows developers to redirect users to a different page or URL. This function is most commonly used when a user submits a form or interacts with a website in some way that requires a response from the server.

The "wp_redirect" function takes two arguments: the first is the URL that you want to redirect the user to, and the second is an optional parameter that can be used to specify the HTTP response code that should be sent back to the browser. By default, the function sends a "302 Found" response, which is typically used for temporary redirects. However, you can also specify a "301 Moved Permanently" response if you want to permanently redirect the user to a different URL.

Here’s an example usage code:

<?php
// Redirect the user to the homepage with a 301 response
wp_redirect( home_url(), 301 );
exit;
?>

In this example, we’re using the "home_url" function to get the URL of the site’s homepage, and then passing that URL to the "wp_redirect" function along with the "301" response code. Finally, we call the "exit" function to terminate the script and ensure that the redirect takes effect.

Overall, the "wp_redirect" function is a useful tool for WordPress developers who need to redirect users to different pages or URLs in their applications.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.