How to redirect WordPress user if not logged in

In today’s article I will go over customizing WordPress to redirect WordPress user to a specific page if the user is not logged in.

Redirecting a WordPress user to a specific page or location is helpful for specific situations. The most common of them is the need to redirect user to a login page if they aren’t currently logged in. Other scenario is when the WordPress site isn’t private, but many of the pages need to be accessible to authenticated customers only.

In the modern world most of the interactive features nowadays are based on user authentication. This is because authentication helps in maintaining a record of each and every action that is performed through a website. Apart from maintaining records, it also helps in providing security to the data that is contained within it. This means that only authenticated users will be able to access important information required for doing tasks. Some of the common actions that are based on user authentication include creating a new posts, commenting, uploading files and many more. If a user who is not logged in tries to perform any of these actions then they redirect to a specific error page or custom login page.

wordpress redirect if not logged in

The redirect plugin is one of the best ways, especially if you want all users browsing your site to follow a certain link. The following examples however shows you how you can redirect a user to a specific page if the user is not logged in with your WordPress website programmatically. This will be accomplished using some common WordPress hooks and PHP code. Most of this code is suitable for being added to your theme’s functions.php file.

The simples usage is if you want to redirect not logged in users to the default WordPress authentication page. This can be easily achieved with the following code snippet:

This code check is the current user is not logged in and if true they are redirected to the default WordPress login page.

If however we want to redirect to a specific page instead, we can use the build in WordPress : wp_redirect(). This function takes three parameters location, status and redirected by. In our usage we are only going to use the location parameter that is used to set the page we want to redirect to. Here is a code snippet that will redirect a user to a specific page only if the user is not logged in and they are currently visiting a specific WordPress page:

 

First we hook our new function to “admin_init” and ten we check if the user is logged in and if he currently is on specific page. If the south conditions are met then we use the “wp_redirect” function to redirect the user to the custom page the we want to redirect to. Also it is important that we for the “wp_redirect” function to exit afterwards.

Alternatively you can hook into the “template_redirect” action hook. The “template_redirect” hook executes just before WordPress determines which template page to load. So it is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.

To add this code snippet to your WordPress website you could install a snippet plugin or make changes through the admin dashboard. But both of these approaches can can work and be safe from updates to core WordPress functionality and theme. The best (and simplest) way to make changes is by adding custom code to your child theme’s functions.php file.

Conclusion

In this tutorial we looked at a quick programmatic approach to redirecting users to a certain page on your site when they are not logged in.

Supercharge your WordPress user management