admin管理员组

文章数量:1319483

Building a plugin that I need to get the users info, more especially the userID

Class userinfo {
    public function __construct() {
        add_action( 'plugins_loaded', array( $this, 'get_user_info' ) );
    }
    
    public function get_user_info() {
        if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            if ( ! ($current_user instanceof WP_User) )
                return;
        }
        return $current_user->user_info;
    }
}

$user = new userinfo();
echo $user->current_user; #echo for testing purposes

PHP comes back with the undefined notice and the whole thing breaks, so what am I doing wrong?

Notice Undefined property: userinfo::$current_user

EDIT Found a mistaken when I logged out of the site so updating here

Class userinfo {
    public function __construct() {
        add_action( 'plugins_loaded', array( $this, 'get_user_info' ) );
    }
    
    public function get_user_info() {
        if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            if ( ! ($current_user instanceof WP_User) )
                return;
        return $current_user->user_info;
        }
    }
}

$user = new userinfo();
echo $user->current_user; #echo for testing purposes

本文标签: trying to get user info in plugin