The get user role get_current_user_role function, and the metatest_init function checks the user's role on the "Administrator" and creates meta tags. But something is not working here .... Help me figure out what

add_action('add_meta_boxes', 'metatest_init'); function metatest_init() { $current_user_role = get_current_user_role( $user ); if ($current_user_role == 'Administrator') { add_meta_box('metatest', 'MetaTest-параметры поста', 'metatest_showup', 'post', 'side', 'high', 'default'); }; } function get_current_user_role( $current_user ) { global $wp_roles; $roles = $current_user->roles; $role = array_shift( $roles ); return $wp_roles->role_names[$role]; } 

    1 answer 1

    The problem is in

      $current_user_role = get_current_user_role( $user ); if ($current_user_role == 'Administrator') { 

    First, get_current_user_role does not need to pass a variable. By default this is the current user.
    Secondly, the syntax for receiving data is incorrect. See documentation or in Russian .
    Well, in the main. It is better to check not the name of the role, the capabilities - Capabilities . For this there is current_user_can ( in Russian )