0

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

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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