wp_redirect

Home » Hooks » wp_redirect

The "wp_redirect" hook in WordPress is a powerful tool that allows developers to control where a user is redirected after a certain action or event. This hook is primarily used to redirect users to a different page or URL within the WordPress site or even to an external URL.

The "wp_redirect" hook is commonly used when building custom login pages, user registration forms, or when implementing custom actions or filters. By using this hook, developers can easily redirect users to a specific page after a successful login, registration, or any other desired action.

Here is an example usage code that demonstrates how to utilize the "wp_redirect" hook:

function custom_redirect_after_login() {
    wp_redirect( home_url( '/dashboard' ) ); // Redirect to the dashboard page
    exit; // Make sure to exit after redirecting
}
add_action( 'wp_login', 'custom_redirect_after_login' );

In this example, we have a custom function called "custom_redirect_after_login" which is hooked to the "wp_login" action. Whenever a user successfully logs in, this function gets triggered and redirects the user to the dashboard page using the "wp_redirect" function. The "exit" function is then called to ensure that further code execution is stopped after the redirection.

By using the "wp_redirect" hook, developers can easily customize the redirection logic in their WordPress projects and provide a seamless user experience.

Learn More on WordPress.org

WordPress snippets using the wp_redirect hook

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