Leave a Reply

7 comments

  1. 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() )
            auth_redirect();
    });
    

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

    wp_die( 'Nope.', 'Not found', [ 'response' => 404 ] );
    
  2. 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.

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

    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        wp_redirect(site_url('/wp-login.php?action=register'));
        exit();
    }
    
  4. 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

     <?php     
    if (!is_user_logged_in()) {
    ?> 
    <style>
    .lor1{ display:none;}
    .lor1.kun{ display: block;}
    .lor1.user_not_login{ display: block;}
    </style>
    <?php }  ?>
    
  5. 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
    }
    
  6. Add This Function to your functions.php file.

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

    <?php
    add_action('template_redirect','wpse64899_check_if_logged_in');
    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(
                'redirect_to',
                get_permalink($pageid),
                site_url('wp-login.php')
            );
            wp_redirect($url);
            exit;
        }
    } ?>
    
  7. 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 ); 
      exit;
        }
    }
    

    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.