Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active October 22, 2015 08:26
Show Gist options
  • Save khoand0000/8d5b605c8d95a401ed7f to your computer and use it in GitHub Desktop.
Save khoand0000/8d5b605c8d95a401ed7f to your computer and use it in GitHub Desktop.
disable "Add New" button for custom post type
  • My case: I have a custom post type is called custom_post and a new role is manager. I want manager role can edit and list custom_post but can't add new custom_post, manager don't have permission for post (don't see post).
  • I used the code when register custom_post type
'capabilities' => ["create_posts" => "create_custom_posts"]

and did't add create_custom_posts, edit_posts caps to manger role

  • Problem: user has manager role can edit custom_post by access /wp-admin/post.php?post=9873&action=edit url, but he/she can't list custom_post by access /wp-admin/edit.php?post_type=custom_post.
  • I found down the cause is that manager role must have edit_posts cap to access /wp-admin/edit.php?post_type=custom_post
  • Solutions:
    • add create_custom_posts cap to manager role. But it is not right for my purpose
    • add edit_posts cap to manager role. It satisfy my purpose but manager users will see Post menu.
    • add edit_posts cap to manager role and remove post && comment menu.
function disable_new_posts() {
    $user = wp_get_current_user();
    if ($user->roles[0] == 'manager') {
        remove_menu_page('edit.php');
        remove_menu_page('edit-comments.php');
    }
}
add_action('admin_menu', 'disable_new_posts');

and make redirect when manager users access /wp-admin/edit.php or /wp-admin/edit-comments.php or /wp-admin/post-new.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment