• Hi there,

    I have a plugin (self written, basic) in my site that contains a function to redirect users based on their role. I have added an ‘add_action’ inside the plugin that hooks ‘wp_login’ in order to have the redirect happen when a user logs in. All good and all works just fine.

    Today I came to call my function from my wordpress header (i realised that if an already logged in user arrives on the login page then I also need to trigger the redirect). So I set up an if statement to make sure the user was logged in and also to check that they were on the login page, and then added my own hook in order to be able to do another ‘add_action’ from my plugin. This all works just fine aswell.

    I got to the plugin and added my ‘add_action’, attached it to my hook and now I am getting the error ‘Trying to get property of non-object’.

    The line in question is:
    echo $role_name = $user->roles[0];
    Which I understand is an array and not an object. However, this line works just fine when I hook it with ‘wp_login’, but gives the error when I hook it with my own hook. So my question is, why does this work with the ‘wp-login’ hook, but not with my own hook that I am setting in the header.php?

    I’m a PHP beginner, so please go easy on me!

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Apparently your own hook is not passing an object to your callback. I know, restating the obvious. You need to get the WP_User object from something that returns such an object, like wp_get_current_user(). Without seeing the related code it’s difficult to explain how this might be corrected.

    FYI, “wp_login” is not the best action to redirect logins from. You may be short circuiting some finishing touches of the login process. You should consider the “login_redirect” filter, which is specifically for this purpose. The WP_User object is still passed after a successful login, except it’s now the third parameter. If you get a WP_Error object here, the login failed for the reason saved in the object, though for security reasons, the specifics of a failed login should not be disclosed to the user.

    This filter only fires from the login page. If the login is from a pop-up form due to the auth cookie expiring, one would normally expect the user to be returned to where ever they were when they were interrupted.

    Another thing, you don’t necessarily have to add your own action hook. You could just call the desired action directly. Adding an action hook is done when you would like to allow other devs to extend your plugin’s functionality by adding in their own callbacks. It’s good thing to do at certain points in your code, but you don’t have to do it just to access your own functions.

    Thread Starter salsabunny

    (@salsabunny)

    Included in header.php:

    if (is_user_logged_in() && (is_page('login'))){
      global $user;
      global $user_login;
      do_action('call_my_redirect_user_by_role',$user_login, $user);
    }

    Plugin:

    add_action('wp_login','my_redirect_user_by_role', 10, 3);
    add_action('call_my_redirect_user_by_role','my_redirect_user_by_role', 10, 3);
    
    function my_redirect_user_by_role($user_login, $user)
    {       
      $role_name = $user->roles[0];
    
      if ( 'administrator' === $role_name ) {
        wp_redirect( site_url('/main-page'));               
        exit;
      } 
      if ( 'manager' === $role_name ) {
        wp_redirect( site_url('/info-page'));               
        exit;
      } 
    }
    • This reply was modified 7 years, 11 months ago by salsabunny.
    Moderator bcworkz

    (@bcworkz)

    The global $user is unreliable to have a proper WP_User object. Use wp_get_current_user() to get the right user. You should verify what’s returned is not zero. A zero indicates there is no logged in user.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trying to get property of non-object error’ is closed to new replies.