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!';



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:

is_admin() || add_action( 'template_redirect', function() {
    if ( ! is_user_logged_in() )

If you want to send a 404 status instead, you can replace the auth_redirect() with:

wp_die( 'Nope.', 'Not found', [ 'response' => 404 ] );
  • 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. – fuxia Dec 17 '12 at 14:13
  • I think it can be even simpler removing is_user_logged_in() and leaving only auth_redirect() because auth_redirect() works as follow: if the user is logged in it does nothing. If the user is not logged in it redirects him/her to login page. – cybmeta May 7 '14 at 15:57
  • 1
    @cybnet Without that extra check, it fails sometimes. – fuxia 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.


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

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

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 }  ?>

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

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(
} ?>

Add this code in your Child's themes of Parent themes function.php file

add_action( 'template_redirect', 'redirect_to_specific_page' );

function redirect_to_specific_page() {

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

wp_redirect( 'https://example.com/login', 301 ); 

Please replace "home" with the page slug name, i.e this is the page you don't want your users to access without logging in and replace this url "https://example.com/login" with the destination url where you want to redirect your users if they are not logged in, i.e this url should direct to your login or registration page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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