3
votes

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 user 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 user clicked on login he was redirected 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 user 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.
3

3 Answers

3
votes

To redirect the user from homepage to login page, you can use Wordpress function is_user_logged_in,

https://developer.wordpress.org/reference/functions/is_user_logged_in/

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

To redirect user on homepage rather Dashboard, you can follow already posted solution on Stack overflow.

https://wordpress.org/support/topic/how-can-i-redirect-users-to-the-front-page-after-log-in

2
votes

Here are a set of codes that you'll need:

  • You need to redirect users from wp-admin to the home page but make sure you do not redirect Ajax requests as well.
  • Disable admin bar for specific user roles

--

//Redirect user to the home page when they try to access admin panel while allowing Ajax requests.
function block_admin_access_for_subs()
{
    // Restrict backend access for subscribers
    if( is_admin() && !defined('DOING_AJAX') && (current_user_can('subscriber')) ){
        wp_redirect(home_url());
        exit;
    }
}
add_action('init', 'block_admin_access_for_subs');

//Disable admin bar for specific user roles
add_action('after_setup_theme', 'remove_admin_bar_for_subs');
function remove_admin_bar_for_subs() {
    if (current_user_can('subscriber') && !is_admin()) {
        show_admin_bar(false);
    }
}

Add below code in the template where you want a user to be redirected to login page.

if ( !is_user_logged_in() ) {
       auth_redirect();
}

Source: https://codex.wordpress.org/Function_Reference/auth_redirect

0
votes

Try to do something like this by passing a url

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

and your redirect.. must be an absolute..

$redirect = site_url().'/login';

or whatever the url u choose.