Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

Thanks.

share|improve this question
add comment

6 Answers

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
add comment

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
add comment

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();
}
share|improve this answer
add comment

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 }  ?>
share|improve this answer
add comment

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 comment

This Function Add on your function.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($pagid),
            site_url('wp-login.php')
        );
        wp_redirect($url);
        exit;
    }
} ?>
share|improve this answer
add comment

Your Answer

 
discard

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.