WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I disable access non registered users? If the user is not logged in I would like to redirect them to a custom registration/login page. Is it possible this using below code as I dont want to use plugin.

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';


share|improve this question

Write this into a plugin:

add_action( 'template_redirect', 'auth_redirect' );

As plugin on GitHub.

This will force all visitors login if they aren’t already.

In some cases, this is asking for a log-in every time. This might work better:

add_action( 'template_redirect', function() {
    is_user_logged_in() || auth_redirect();
share|improve this answer
Hi, Is there any way to redirect it to a specific page? – FlourishDNA Dec 17 '12 at 13:54
By default the visitor will be redirected to the page she tried to see. You could write a wrapper very similar to auth_redirect() and set $login_url to a specific page. – toscho Dec 17 '12 at 14:13
Love the simplicity here! – Matthew Boynes Dec 17 '12 at 20:48
Uuuuuuh... +1 would give +100 for such a smart one liner :) – kaiser Jun 22 '13 at 22:33
@cybnet Without that extra check, it fails sometimes. – toscho May 7 '14 at 16:01

If you don't feel like changing your code, you could use this plugin instead: Restricted Site Access. It's highly rated and in my personal experience, it works really well.

Limit access your site to visitors who are logged in or accessing the site from a set of specified IP addresses. Send restricted visitors to the log in page, redirect them, or display a message or page. A great solution for Extranets, publicly hosted Intranets, or parallel development / staging sites.

share|improve this answer

You probably just have to put the following in your functions.php:

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
share|improve this answer

I got the Answer its easy by css..

  1. main div(.lor1) means comman div start after header and finesh before footer
  2. Add new class (.lor1.kun) in main div when user loging this main class display: block;
  3. make new html Ex. display mess loging frist one (.lor1.user_not_login)

ADD this code in header.php

if (!is_user_logged_in()) {
.lor1{ display:none;}
.lor1.kun{ display: block;}
.lor1.user_not_login{ display: block;}
<?php }  ?>
share|improve this answer

Well, your code is actually functional. You just have to call the right modules in the right places.

if( !is_user_logged_in() ){
    // show_registration_panel() 
} else {
   // He is a registered user. Proceed as usual
share|improve this answer

Add This Function to your functions.php file.

when user not login access on home page(page id ==2) to redirect on login page.

function wpse64899_check_if_logged_in()
    $pageid = 2; // or whatever you want it to be
    if(!is_user_logged_in() && is_page($pageid))
        $url = add_query_arg(
} ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.