admin管理员组

文章数量:1327843

I want to add more post type and more role to the code below. The code is working great for single post type or role, any idea how to do that without breaking its function?

//Remove CSS custom post-types GRUP
add_action('admin_head', 'grup_custom_admin_post_css');
function grup_custom_admin_post_css() {
    $current_user = wp_get_current_user();
      if ( $current_user->roles[0] === 'custom_role' )
    
    global $post_type;

    if ($post_type == 'custom_post_types') {
        echo "<style></style>";
                   
    }
}

Thanks for your help

I want to add more post type and more role to the code below. The code is working great for single post type or role, any idea how to do that without breaking its function?

//Remove CSS custom post-types GRUP
add_action('admin_head', 'grup_custom_admin_post_css');
function grup_custom_admin_post_css() {
    $current_user = wp_get_current_user();
      if ( $current_user->roles[0] === 'custom_role' )
    
    global $post_type;

    if ($post_type == 'custom_post_types') {
        echo "<style></style>";
                   
    }
}

Thanks for your help

Share Improve this question asked Jul 31, 2020 at 12:00 jasawebjasaweb 519 bronze badges 2
  • 1 This looks like a basic PHP question, does a variable have one of a list of values, e.g. is the $post_type variable A or B or C or D etc, you don't need WP expertise to answer this, anybody with basic programming knowledge can solve the problem – Tom J Nowell Commented Jul 31, 2020 at 12:42
  • apologize, newbie here and still learning :), do you have another approach for this case? – jasaweb Commented Aug 1, 2020 at 1:02
Add a comment  | 

1 Answer 1

Reset to default 2

its better to include your assets with admin_enqueue_scripts for your question

add_action('admin_head', 'grup_custom_admin_post_css');
function grup_custom_admin_post_css()
{
    $allow_post_types = array('custom_role', 'custom_role_2', 'etc');
    $allow_roles = array('post_type', 'post_typ_2');

    $current_user = wp_get_current_user();
    if (array_intersect($current_user->roles, $allow_roles)) { // do something if user have one of these roles
        global $post_type;

        if ($post_type && in_array($post_type, $allow_post_types)) {
            echo "<style></style>";
        }
    }
}

本文标签: plugin developmentadd more custom post types and custom role to the code