Simple redirection snippets for WordPress

There are several ways to add redirections to WordPress, but today we will look at some very basic redirection methods.

Depending on your use case, you may sometimes find that WordPress redirects users to the wp-login.php page, which is not something you may want your users to see. Instead, by putting redirections in place, you will be able to conceal the default login screen and redirect users to custom login pages or any other page on your website.

To use the snippets on this page you will need to have a child theme installed, If you do not have a child theme installed you can create your own custom snippets plugin following our simple guide.

If you are looking for a specific redirection contact us, we will be glad to help.

Let’s take a look at some simple and useful redirections:

Redirect user to a custom login page

This code defines a function called ‘smart_login_page’ that is hooked into the ‘init’ action in WordPress using the add_action() function.

When a user visits the WordPress login page (wp-login.php) via a GET request, the function checks if the requested URL is ‘wp-login.php’ and the request method is ‘GET’. If both conditions are met, the function redirects the user to the ‘/login’ page using the wp_redirect() function and then terminates further execution of the script using the exit() function.

Additionally, the function checks if the user has been redirected to the login page by checking if the ‘redirect_to’ parameter is present in the URL. If the parameter is present, the function does not redirect the user to the ‘/login’ page.

To redirect a user simply change /login to any custom page on your website.


function smart_login_page() {
    $login_url  = home_url( '/login' ); // Change to custom page
    $url = basename($_SERVER['REQUEST_URI']); 
    isset( $_REQUEST['redirect_to'] ) ? ( $url   = "wp-login.php" ): 0;
    if( $url  == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET')  {
        wp_redirect( $login_url );

Redirect user when logging out of WordPress

When logging out of WordPress, you are taken back to the wp-login.php page. To alter this behaviour, use the snippet below. Replace “” with the URL that you wish to be redirected to when logging out of WordPress.

add_action('wp_logout', 'smart_after_logout');

function smart_after_logout(){
         wp_redirect( '' ); // Change URL

Redirect user to another page if logged in

If you would like to redirect a user from one page to another whilst they are logged in you can use the following snippet.

The ‘smart_page_redirect’ function first checks if the current page being accessed has a slug of ‘page-slug’ and if the user is currently logged in using the is_page() and is_user_logged_in() functions respectively.

If the current page has a slug of ‘page-slug’ and the user is logged in, the function then uses the wp_redirect() function to redirect the user to the page with the URL ‘’ with a 301 (permanent) redirect status. Finally, the function exits to prevent further execution of the current script.

add_action( 'template_redirect', 'smart_page_redirect' );

function smart_page_redirect() {
    if ( is_page('page-slug') && is_user_logged_in() ) {
        wp_redirect( '', 301 ); 

If you would like to do the reverse and only redirect a user if they are not logged in use this snippet instead.

add_action( 'template_redirect', 'smart_page_redirect_out' );

function smart_page_redirect_out() {
    if ( is_page('page-slug') && ! is_user_logged_in() ) {
        wp_redirect( '', 301 ); 

Redirect user when logging in with WooCommerce

This WordPress function adds a filter to the ‘woocommerce_login_redirect’ hook. When this hook is triggered, the ‘smart_login_redirect’ function will be executed with a priority of 9999.

The ‘smart_login_redirect’ function will then replace the $redirect_url variable with the value ‘/page-slug’, which represents the URL of the page to which the user will be redirected after logging in.

Therefore, this code snippet modifies the default behaviour of WooCommerce’s login redirect, redirecting the user to a specific page (/page-slug) instead of the default WooCommerce behaviour of redirecting to the previous page the user was on.

The priority of 9999 ensures that the ‘smart_login_redirect’ function is executed last among all other functions that may be hooked into the ‘woocommerce_login_redirect’ filter. This is useful when there are multiple filter functions hooked into the same filter, and you want to make sure that your filter function has the final say in modifying the output.

add_filter( 'woocommerce_login_redirect', 'smart_login_redirect', 9999 );
function smart_login_redirect( $redirect_url ) {
    $redirect_url = '/page-slug';
    return $redirect_url;

Using a redirection plugin

If you do not want to get your hands dirty with code you can use the Redirection plugin developed by John Godley to create any type of redirection you like.

The plugin allows you to quickly and easily create and manage redirects without requiring any knowledge of Apache or Nginx.

Redirection provides complete support for regular expressions, enabling you to create redirect patterns that match any number of URLs. You can also match query parameters and pass them through to the target URL.

The plugin also features conditional redirects that enable you to redirect users based on a variety of conditions such as login status, browser, referrer, IP address, page type, and many others.

WordPress Maintenance from £25.99/month

Experience unparalleled WordPress support and maintenance with our exceptional service plans. Entrust us with all your WordPress needs, guaranteeing the seamless operation and upkeep of your website.

Get Started