WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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(){
        $url = get_bloginfo('home').'/login';
        header('HTTP/1.1 403 Forbidden');
        header("Location: $url");

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:

$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


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.