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.

In my application, users have to login first before accessing the website.

So now what I am doing, I have created one custom login page.

How can I redirect a user to the login page if he/she is not logged in yet.

Should i customize wp-login.php for login? Which is better way, 1. Redirect user to login page or 2. wp-login.php

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can use the following action to redirect all traffic to that login page. You will need to specify the page name and the page slug.

add_action('wp', 'wpse_41699_wp', 0);
function wpse_41699_wp(){
    if(is_page('Login'))
        return;
    if(!is_user_logged_in()){
        $url = get_bloginfo('home').'/login';
        header('HTTP/1.1 403 Forbidden');
        header("Location: $url");
        exit;
    }
}

Also, you can download the plugin version for this answer here.

share|improve this answer
    
Thanks Brian Fegter –  Ajay Patel Feb 9 '12 at 6:48

There are many ways to do this, you can try something like this:

<?php
$post = $wp_query->post;

if ( is_user_logged_in() ) {
                include (TEMPLATEPATH . '/logged.php'); 
            } else {
                include (TEMPLATEPATH . '/nonlogged.php'); 
            }
 ?>

and of course, nonlogged.php may be a page, or you can use jquery to show a pop up or whatever

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.