How to Redirect User If Not Logged in WordPress » Page Redirect

How to Redirect If Not Logged In WordPress In the last post, I demonstrated how to check if the user is logged in in WordPress.  One common reason why you may want to check if WordPress user is logged in is to show different pages based on the logged-in status of the WordPress user. If you want to redirect users if not logged in WordPress, you can achieve this by adding a code snippet in your functions.php file as you will see in this quick tutorial.

Why redirect users in WordPress?

Redirecting users is an important requirement for most WordPress sites. There are different times you may want to redirect users from one page to another.  For example, you can redirect WooCommerce users after checkout, or you can redirect the user after registration or redirection logging into the website.

To redirect if not logged in WordPress is one of the most common redirections. While there are plugins that can help manage redirection in WordPress but adding a small snippet that solves this problem is a better solution than relying on a third-party plugin.

In this tutorial, I will demonstrate how to add a snippet in your theme or plugin that creates the redirect for users who are not logged in to a page of your choice.

Steps to Redirect if Not Logged in WordPress

To implement the redirect if not logged in WordPress, you should following the following steps :

  1. Login to your WordPress site and navigate to the theme editor under the appearance menu on the main dashboard menu.
  2. Open the functions.php file of the active WordPress theme. You can also use the CPanel or FTP to access the active theme functions.php file. It is located on this path – wp-root-installation/wp-content/themes/active-theme/functions.php
  3. Create an action hook that hooks on the admin initialization hook – admin_init , for example add_action(‘init’, ‘redirect_if_not_logged_in’) . This hook has a callback function that will have the logic to check if the user is logged in WordPress and if not logged in they are redirected to the page we wish.
  4. Create the callback function as we have mentioned above and add the code accordingly.
  5. Save the changes and go to the frontend to test if the users who are not logged in are redirected to the page you added in the code.

Let us demonstrate how you can add redirect if the user is not logged in WordPress. I will be using the default WordPress theme on the localhost installation but this can work for all other sites, irrespective of the theme and the hosting environment.

The code to add redirect may also take another condition like in most cases when you want to restrict content from non-logged users, you would check two conditions:

When these conditions are met, you should use the wp_redirect() function to redirect the user to the page of your choice.

WP Function : wp_redirect()

This is a WordPress redirection function that can be used in themes and plugin to redirect users. The general expression of this function is as follows:

wp_redirect( string $location, int $status = 302, string $x_redirect_by = 'WordPress' )

As you can see the function has three parameters that are as follows:

Parameter Description
$location This is the URL where you want to redirect the user. When you use this function when the user is not logged in, you need to provide the URL to the page we will redirect the user as you will see in the code sample
$status This is an optional parameter and you may not see it used often but it’s the HTTP response status code to use. Default ‘302’ and you can use 301 for a permanent redirect. Be careful when you use this parameter with the 301 redirect option.
$x_redirect_by This is simply the application applying the redirection and this will rarely change the default value which is WordPress

Template Redirect Action If User is not logged in

To redirect the user who is not logged in in the WordPress you can now use the wp_redirect function with the conditional check if the user is logged in as follows:

add_action( 'template_redirect', 'redirect_if_user_not_logged_in' );

function redirect_if_user_not_logged_in() {

	if ( is_page('slug || ID') && ! is_user_logged_in() ) { //example can be is_page(23) where 23 is page ID

		wp_redirect( 'http://your-redirect-page-here '); 
 
     exit;// never forget this exit since its very important for the wp_redirect() to have the exit / die
   
   }
   
}

Replace the slug of the conditional check with your respective page and also replace the redirect URL in the code above and add it to the functions.php to redirect the users that are not logged in to that page URL.

Admin Init Redirect Action if User is not Logged in

Alternatively, you can hook on the admin_init instead of the template_redirect to redirect the user if not logged in WordPress.

The code should be the same apart from the point of adding the action hook. The code should be as follows:

add_action( 'admin_init', 'redirect_if_user_not_logged_in' );

function redirect_if_user_not_logged_in() {

if ( !is_user_logged_in() && is_page('slug') ) {

      wp_redirect( 'http://your-redirect-page-here '); 
   
   exit;
   }
}

When we add this code to the default WordPress theme functions.php and add the slug for the page we want to create the redirection and the URL for the destination, it works perfectly as you can see:

How to Redirect If Not Logged In WordPress

Final Thoughts

When you want to create redirect if the user is not logged in WordPress, you need to first check if the user is logged in using the code I shared in that post, then use the wp_redirect() function to add the new destination where users should be redirected if they access a certain page or post.

This code can be added to the functions.php file of your theme or you can use it as part of your custom WordPress plugin. It is important to always remember, when you use the wp_redirect function, you should NEVER forget to add the exit since this function does not automatically exit.

As a WordPress plugin developer, you may also consider another WordPress redirection plugin wp_safe_redirect() which provides a better way to redirection since it checks if the $location parameter host is allowed.

Similar Articles

 

Comments are closed.