0

I am new to wordpress. I am running wordpress setup on http://localhost/wordpress/. I am facing two problems right now:

  • Only the logged in users can access the site. So, I am trying to redirect the guest from home page to login page using the following code which is somehow isn't working:

Path: wp-content/themes/twentysixteen/header.php

        <?php
            if(get_permalink() != wp_login_url() && !is_user_logged_in()){
                wp_redirect( wp_login_url() ); exit;
            }
        ?>

Since the above code wasn't working, I tried to move on by letting the user to login manually by clicking on the login button. Here is the working code:

<?php
    if(get_permalink() != wp_login_url() && !is_user_logged_in()){
        // wp_redirect( wp_login_url() ); exit;
?>
        <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>
<?php
    }
?>
  • From above code, when guest clicked on login he/she was redirecting to login page and When the user is logs in, the page is redirecting to wordpress admin i.e wp-admin instead of home page i.e http://localhost/wordpress.

What I am trying to do is:

  • Redirect the guest from home page to login page, if the user isn't logged in.
  • And then redirect the user from login page to home page instead of wp-admin, when user logs in.
  • When you talk about "home page" do you really refer to "home" (i.e. example.com) or to any other page in the "front end"? – cybmeta May 19 '16 at 6:37
  • @cybmeta I meant http://localhost/wordpress. – Mr_Green May 19 '16 at 6:38
  • @cybmeta guest users can't access any page of the site. When guests visit http://localhost/wordpress, they must be redirected to login page. and when the log in, they must be redirected to http::localhost/wordpress. Apart from the above description, if the guest is on other page than the home page for example http://localhost/wordpress/about then the guest will still be redirected to login page and when he/she logs in, they will be redirected to http://localhost/wordpress/about. – Mr_Green May 19 '16 at 6:45
  • Ok. Now it is clear. So, the user should be redirected to the page he/she was trying to see as guest. – cybmeta May 19 '16 at 6:46
  • @cybmeta yes I meant the same :) – Mr_Green May 19 '16 at 6:49
2

If I understood correctly, you need to check if the user is logged in, if not, redirect it to login page. If the user logs in succesfully, he should be redirected to the page he was trying to see. You can do this by using the redirect argument of wp_login_url().

This code should work (not tested):

add_action( 'init', 'cyb_restrict_guest_access' );
function cyb_restrict_guest_access() {
    global $wp;
    if( ! is_user_logged_in() && ! cyb_is_login_page() ) {
        wp_redirect( wp_login_url( site_url( $wp->request ) ) );
        exit;
    }
}

function cyb_is_login_page() {
    return in_array($GLOBALS['pagenow'], array('wp-login.php'));
}
1

You can use following code to check home page or not:

        if(is_home() && !is_user_logged_in()){
            wp_redirect( wp_login_url() ); exit;
        }
  • I am trying the same as you can see in my above code. Somehow it isn't working. – Mr_Green May 19 '16 at 6:46
1

This in your header.php or before the get_header() call in any template will redirect a user who isn't logged in and who tries to reach any page of your site to the login page:

if( !is_user_logged_in() ) {
    wp_redirect( wp_login_url() );
    exit;
}

This will redirect all users to the home page after login:

function tbdn_login_redirect( $redirect_url ) {

    return home_url();

}

add_filter( 'login_redirect', 'tbdn_login_redirect' );

Note that it doesn't restrict a logged in user from accessing admin screens if they know the URL or if their login has the admin bar enabled.

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.