Remove edit or add facility for custom post type

Is it possible to remove all references to ‘edit’ or ‘add’ from a particular post type on its post list display screen in admin?

So basically what I need is that for this post type, all roles defined, whatever they are, can only view a list of these posts, and not do anything with them except trash them. So that means removing the ‘Add New myposttype’ button, as well as the ‘Edit | Quick Edit’ links beneath each entry of the list posts screen.

Read More

In my custom post type these are my capability related settings:

        'capability_type' => array('food_item','food_items'),
        'map_meta_cap'    => true,
        'capabilities' => array(
                        'publish_posts' => 'publish_food_items',
                        'edit_posts' => 'edit_food_items',
                        'edit_others_posts' => 'edit_others_food_items',
                        'delete_posts' => 'delete_food_items',
                        'delete_others_posts' => 'delete_others_food_items',
                        'read_private_posts' => 'read_private_food_items',
                        'edit_post' => 'edit_food_item',
                        'delete_post' => 'delete_food_item',
                        'read_post' => 'read_food_item'

In my plugin init function, I’m adding the following:

    global $wp_roles;
    $wp_roles->add_cap( 'administrator', 'edit_food_item' );
    $wp_roles->add_cap( 'administrator', 'edit_food_items' );

At this point the administrator just cannot see the food items entry in the menu at all.

Related posts

Leave a Reply

3 comments

  1. For say the ‘moomin’ post type, when defining the custom post type specify its ‘capability_type’ as ‘moomin’.

    This will give you the ‘capabilities’ edit_moomin’ etc which you can then remove from individual roles, e.g.:

    global $wp_roles;
    // remove capability edit_moomin from role editor
    $wp_roles->remove_cap( 'editor', 'edit_moomin' );
    

    @Simon Forster claims readonly posts IS possible, if so then this should do the job:

    $wp_roles->add_cap( 'editor', 'read_moomin' );
    
  2. As requested…

    add_action( 'init', 'create_my_post_types' );
    
    function create_my_post_types() {
        register_post_type(
            'name_of_your_post_type_singular',
            array(
                'public' => true,
                'capability_type' => 'name_of_your_post_type_singular'
                ),
            )
        );
    }
    
    add_action('admin_init', 'give_user_read', 10, 0);
    function give_user_edit() {
        if(current_user_can('edit_others_posts')) {
            global $wp_roles;
            $wp_roles->add_cap('author','read_name_of_your_post_type_plural' );
            $wp_roles->add_cap('editor','read_name_of_your_post_type_plural' );
                    etc. etc.
        }
    }
    

    I’ve added the read capability to the user roles. As I have not given anyone edit or delete.. they won’t have it. As I’ve given author and editor read access, they will have it.

    In case you’re not sure what the capabilities will be, when you add the type as I have above you’ll generate the capabilities below

    'capabilities' => array(
                'publish_posts' => 'publish_name_of_your_post_type_plural',
                'edit_posts' => 'edit_name_of_your_post_type_plural',
                'edit_others_posts' => 'edit_others_name_of_your_post_type_plural',
                'delete_posts' => 'delete_name_of_your_post_type_plural',
                'delete_others_posts' => 'delete_others_name_of_your_post_type_plural',
                'read_private_posts' => 'read_private_name_of_your_post_type_plural',
                'edit_post' => 'edit_name_of_your_post_type_singular',
                'delete_post' => 'delete_name_of_your_post_type_singular',
                'read_post' => 'read_name_of_your_post_type_singular'
    )
    
  3. You could remove it by using some css in your plugin within the admin area

    In your functions.php file link to some CSS (reference: http://codex.wordpress.org/Function_Reference/wp_enqueue_style)

    add_action('admin_enqueue_scripts', 'my_admin_script');
    function my_admin_script()
    {
    wp_register_style( 'myPluginStylesheet', plugins_url('stylesheet.css', __FILE__) );
    }
    

    The CSS:

    a[href="post-new.php?post_type=events"],
    #adminmenu a[href="edit.php?post_type=events"],
    #adminmenu a.editinline,
    #adminmenu a[title="Edit this item"] {
    display:none;
    opacity:0;
    }
    

    In the CSS you’ll need to replace the references to ‘events’ with the name of your custom post type. This will work IE7+ I believe using those selectors.