admin管理员组

文章数量:1122846

I am doing custom registration and logging and assigning a custom user role.

I register like this:

$user_id = wp_create_user($username, $password, $email);

            if (!is_wp_error($user_id)) {
                $user = new WP_User($user_id);
                $user->set_role('user');

                echo __('Registration successful.', 'vk-add-and-review');
            } else {
                echo __('Registration failed.', 'vk-add-and-review');
            }

In wp_usermeta wp_capabilities I have this: a:1:{s:4:"user";b:1;}

Still, when I check for the user after logging in it is empty:

if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            if (in_array('user', (array) $current_user->roles)) {
                return '<p>' . __('You are logged in as a user.', 'vk-add-and-review') . '</p>';
            } else {
                return '<p>' . __('You are logged in, but not as a user.', 'vk-add-and-review') . '</p>';
            }
        }

$current_user->roles is an empty array

本文标签: User has empty instead of custom role when logged in