简体   繁体   中英

Hide custom post type link in wordpress admin menu from Editors

I have a custom post type called "Services" and I would like the custom post type link to show for Admins ONLY and not Editors.

I am aware that I can use "capability_type", however this will not work for me as I don't want to disable the "Services" custom post type from Editors completely. I am still linking to the "Services" custom post type from a custom admin page, so I still want the Editors to be able to access it. I just don't want the automatically created link to show up for Editors in the admin menu, however the automatically created link must still show for Admins.

I thought maybe there is a way to put a condition around the following line, but I don't know php that well so I don't know if it can be done.

'show_in_menu' => true

Below you can find the code for my custom post type.

register_post_type( 'services',
    array(
      'labels' => array(
        'name' => __( 'Services' ),
        'singular_name' => __( 'Service' )
      ),
      'public' => true,
      'has_archive' => false,
      'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
      'rewrite' => array('slug' => 'services'),
      'show_in_menu'      => true
    )
  );

Try this one by adding capabilities

register_post_type('services', array(
    'labels' => array(
        'name' => __('Services'),
        'singular_name' => __('Service')
    ),
    'public' => true,
    'has_archive' => false,
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
    'rewrite' => array('slug' => 'services'),
    'show_in_menu' => true,
    'capabilities' => array(
        'edit_post' => 'update_core',
        'read_post' => 'update_core',
        'delete_post' => 'update_core',
        'edit_posts' => 'update_core',
        'edit_others_posts' => 'update_core',
        'delete_posts' => 'update_core',
        'publish_posts' => 'update_core',
        'read_private_posts' => 'update_core'
    ),
        )
);

Use these:

function wpse28782_remove_menu_items() {
    if( !current_user_can( 'administrator' ) ):
        remove_menu_page( 'edit.php?post_type=quote' );
    endif;
}
add_action( 'admin_menu', 'wpse28782_remove_menu_items' );

change quote according to your post type name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM