Redirect non logged in user

There are many plugins which can be used for redirection. We can use small code on our theme file rather using plugins. To redirect non logged in member to custom login page or to specific page or to normal login page, we can put the following code on our functions.php inside active theme:

function redirect_non_logged_in(){
if( !is_user_logged_in() && !is_page(128) ) { // if user is not logged
wp_redirect(site_url('/login')); //This redirects to the login page.

For is_user_logged_in, you can get more details from
In is_page(128), 128 is page id. We can use page name such as is_page(‘login’). For more details about is_page, go to codex.



4 Comment

  1. Amy says: Reply

    Do you mind if I quote a couple of your posts as long as
    I provide credit and sources back to your weblog?
    My blog is in the very same niche as yours and my users
    would certainly benefit from a lot of the information you provide here.

    Please let me know if this ok with you. Thanks!

    1. vinay says: Reply

      Hi Amy,
      sure you can if you provide credit source and back to my weblog.

  2. Hey there would you mind stating which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something unique. P.S Apologies for being off-topic but I had to ask!

    1. vinay says: Reply

      I am using wordpress and its custom theme(my own theme from scratch).

Leave a Reply