Trying to get property of non-object error
-
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.
- The topic ‘Trying to get property of non-object error’ is closed to new replies.