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

2 Answers 2


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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.