How to Redirect User If They Are Not Logged In (WordPress)

By December 11, 2016June 20th, 2020Website

“How to redirect user if they are not logged in” is a WordPress question that is commonly asked and is something that is very easy to do without using a plugin
Redirecting a user if they are not logged in is ideal for private and membership sites as you can redirect the user to a specific page (a sales page for example) if they are not logged in.

I personally use this trick on my subdomain training.peterstavrou.com which is a demo site I setup for my YouTube training videos.

 

How To Redirect User If Not Logged In

To achieve this you simply need to add a piece of code in your header.php file.

    1. 1. Login to the back-end of your WordPress site and go to Appearance then Editor.

How To Redirect User If Not Logged In

 

    1. 2. On the left-hand side, under Templates click on header.php

 

  1. 3. Copy and paste the below code at the very top, just under ?php
if (!is_user_logged_in()) {
wp_redirect( 'https://www.peterstavrou.com');
exit;
} ?>

 

For Example:

How To Redirect User If Not Logged In

4. Change the URL https://www.peterstavrou.com to a URL of your choice and click on the Update File button.

That’s all there is to it 🙂

You may also be interested in…

Thrive Architect - Landing Pages
An amazing WordPress plugin to quickly and easily create beautiful, engaging and visually impressive landing pages that convert visitors into sales!

Thrive Leads – The Best Popup Plugin for WordPress

Thrive Leads - The Best PopUp Plugin for WordPress
If you are looking for the best WordPress popup plugin that allows you to create various types of opt-in forms to collect emails and quickly grow your mailing list then I cannot recommend Thrive Leads enough.

Disclosure: This page contains affiliate links. I have a strict policy to only promote products I personally use to run my business. If you decide to purchase a product through an affiliate link I will receive a commission at no extra cost to you. This is how I fund the blog – thanks for your support!

4 Comments

  • Hi Peter,
    I did as above but now if I try and log in it takes me to the redirected page “for logged in users”.
    It won’t allow me to login. Any ideas.
    All I wanted was for those people who have already registered and logged in to be shown a different page from those first timers/not logged in users. I’ve removed it just in case for now.

    • Peter Stavrou says:

      Heya!
      So basically you want to redirect users on login?
      If so, you can use this plugin.

      • Sodiq says:

        Hi Peter,

        I used the code to redirect non-logged in users to a specific general welcome page and it works. However, I want to put a link for registered users to be able to login from that same welcome page, but when the login link is clicked, it returns to the welcome page rather than take the user to the login page. Kindly help. Thanks.

  • Sam says:

    Works perfectly! Thanks!

Share Your Thoughts

 

Do NOT follow this link or you will be banned from the site!