Tell me more ×
WordPress Answers 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

3 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.

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

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

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

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.