0

I'm building a custom settings page in the WordPress Admin Panel and I'm pulling some data from a WP_User object at one point. I've used this object dozens of times in the actual theme, but in this case I am not seeing the same behavior. The "object" is in $user, and when I try to use $user->display_name to retrieve the display name, I am told that I am trying to get a property of a non-object.

Here's the relevant code:

$userID = $assignment->user_id; //existing user ID is passed correctly
$user = get_user_by('ID', $userID); //should work fine...
print_r($user); //clearly shows an WP_User object in the output
echo gettype($user); //returns boolean
echo $user->display_name; //says I am trying to get a property of a non-object

Here's the output of print_r($user):

WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 1
            [user_login] => name
            [user_pass] => $somehash
            [user_nicename] => name
            [user_email] => [email protected]
            [user_url] => http://example.com
            [user_registered] => 2017-11-21 17:05:00
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => bobsmith
        )

    [ID] => 4
    [caps] => Array
        (
            [member] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [0] => member
        )

    [allcaps] => Array
        (
            [activate_plugins] => 
            [create_roles] => 
            [create_users] => 
            [delete_others_pages] => 
            [delete_others_posts] => 
            [delete_pages] => 
            [delete_plugins] => 
            [delete_posts] => 
            [delete_private_pages] => 
            [delete_private_posts] => 
            [delete_published_pages] => 
            [delete_published_posts] => 
            [delete_roles] => 
            [delete_themes] => 
            [delete_users] => 
            [edit_dashboard] => 
            [edit_others_pages] => 
            [edit_others_posts] => 
            [edit_pages] => 
            [edit_posts] => 
            [edit_private_pages] => 
            [edit_private_posts] => 
            [edit_published_pages] => 
            [edit_published_posts] => 
            [edit_roles] => 
            [edit_theme_options] => 
            [edit_users] => 
            [export] => 
            [import] => 
            [install_plugins] => 
            [install_themes] => 
            [list_roles] => 
            [list_users] => 
            [manage_categories] => 
            [manage_links] => 
            [manage_options] => 
            [moderate_comments] => 
            [promote_users] => 
            [publish_pages] => 
            [publish_posts] => 
            [read] => 1
            [read_private_pages] => 
            [read_private_posts] => 
            [remove_users] => 
            [restrict_content] => 
            [switch_themes] => 
            [unfiltered_html] => 1
            [update_core] => 
            [update_plugins] => 
            [update_themes] => 
            [upload_files] => 
            [member] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)

Worst case, I can query the database and pull the value like that, but is there any reason why WP_User doesn't seem to behave the same in the Admin Panel?

11
  • When does this code run? In the admin area, for some things the timing matters, and running on a hook that's too early can mean things rely on stuff that hasn't loaded yet.
    – Tom J Nowell
    Dec 8, 2017 at 1:18
  • Maybe try adding global $user; before calling your code. Depending on where the code is placed, the variable might be out of scope.
    – signal2013
    Dec 8, 2017 at 6:03
  • Paste the output of print_r($user); Dec 8, 2017 at 10:14
  • @signal2013 The code is ran immediately after the variable is declared, so I don't imagine there are scoping issues.
    – AKor
    Dec 8, 2017 at 16:25
  • 3
    @AKor this will do it $user->data->display_name Dec 8, 2017 at 16:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.