0
votes

Apologies for the amateur questions, but I'm struggling a bit in WordPress grateful for any help.

I have built a LMS site using LearnDash. The homepage is simply a login, so I have added the following to functions.php:

function admin_redirect() {
 if ( !is_user_logged_in()) {
   wp_redirect( home_url('login') );
   exit;
 }
}
add_action('get_header', 'admin_redirect');

I have set the front page to be the learner profile. I assumed users would get redirected to this when they login but they drop into the wp-admin dashboard (which I never want non-admins to see).

Because my coding skills are pants, I've installed the 'Peter's Login Redirect' and asked it to redirect users to the home page / front page (which is the user profile page).

I've then tried it across different browsers and sometimes I'm getting to the front page / user profile but sometimes I'm getting dropped back into WP-Admin. I've tried incognito mode, clearing cookies etc.

I'm obviously being a muppet - as what I want is very simple:

  • All users - force login
  • Non-admins - from login go to front page with user profile (via Learndash short code)
  • Admins - from login go to admin dashboard / wp-admin

Grateful, if anyone has the time to help out an amateur trying to learn!

1

1 Answers

0
votes

Instead of using the login redirect plugin, try hooking into login_redirect. Here's an example based on the example in the documentation.

function example_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect Admins to WP Admin
            return home_url('wp-admin');
        } else {
            // redirect everyone else to the home page
            return home_url();
        }
    } else {
        return home_url();
    }
}
add_filter( 'login_redirect', 'example_login_redirect', 10, 3 );