Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been researching this all day and haven't come up with much results.

I want to restrict who can see my Custom Post Types by User Level (or some other way). So if the person logged in is not an 'administrator' the added Custom Post Types are not visible to them.

Here is part of a Custom Post Type that I have:

add_action('init', 'portfolio_register');

function portfolio_register() {

    $labels = array(
        'name' => _x('Case Studies', 'post type general name'),
        'singular_name' => _x('Case Study Item', 'post type singular name'),
        'add_new' => _x('Add New', 'portfolio item'),
        'add_new_item' => __('Add New Case Study Item'),
        'edit_item' => __('Edit Case Study Item'),
        'new_item' => __('New Case Study Item'),
        'view_item' => __('View Case Study Item'),
        'search_items' => __('Search Case Studies'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        /*'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',*/
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail')
      ); 

    register_post_type( 'portfolio' , $args );
}

And here are the potential solutions that I found that don't work for the Custom Post Type menu removal:

function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login != 'admin')
    {
        $restricted = array(__('Posts'),
                            __('Media'),
                            __('Links'),
                            __('Pages'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins'),
                            __('Users'),
                            __('Tools'),
                            __('Settings')
        );
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }// end while

    }// end if
}
add_action('admin_menu', 'remove_menus');

The above works to remove the default menu items but I couldn't get it to remove a Custom Post Type menu. Plus it is username specific, which is ok if I could add more than one user to it.

global $user_login;
get_currentuserinfo();
   if (!current_user_can('update_plugins')) {
      .......
   }

The above didn't work at all.

Thanks.

share|improve this question

3 Answers 3

Codex - Register Post Type

See the capability_type and capabilities arguments for register_post_type. You can pass the capabilities argument an array of capabilities to map to the necessary caps, here's an example of the args array with custom capabilities.

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capabilities' => array(
        'publish_posts' => 'ADD_CAP_HERE',
        'edit_posts' => 'ADD_CAP_HERE',
        'edit_others_posts' => 'ADD_CAP_HERE',
        'delete_posts' => 'ADD_CAP_HERE',
        'delete_others_posts' => 'ADD_CAP_HERE',
        'read_private_posts' => 'ADD_CAP_HERE',
        'edit_post' => 'ADD_CAP_HERE',
        'delete_post' => 'ADD_CAP_HERE',
        'read_post' => 'ADD_CAP_HERE',
    ),
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail')
); 

You'd of course replace ADD_CAP_HERE with a capability. If you wanted to limit this post type to admins, simply use a capability only admins have, such as manage_options.

Table of roles and their caps(for quick reference).
http://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table

share|improve this answer
    
Could you please explain ADD_CAP_HERE? I need to hide the menu of of my custom post type from editors; Your code didn't help on hiding the menu item :( –  numediaweb Jul 20 at 21:07

Ok, after doing some more Googling I finally found an answer and combined it with one of the scripts above to create what I'm looking to do. Below is what i've done just in case someone else is looking to do the same:

global $user_login;
get_currentuserinfo();
if (!current_user_can('update_plugins')) {
    .....
}

The above code will get the current users level and if they have permissions to update/edit plugins then they have access to the enclosed functions.

add_filter( 'custom_menu_order', 'toggle_custom_menu_order' );

function remove_those_menu_items( $menu_order ){
    global $menu;

    foreach ( $menu as $mkey => $m ) {
        $key = array_search( 'edit.php?post_type=portfolio', $m );
        $keyB = array_search( 'edit.php?post_type=bio', $m );
        $keyC = array_search( 'edit.php?post_type=philo', $m );

        if ( $key || $keyB || $keyC )
            unset( $menu[$mkey] );
    }

    return $menu_order;
}
add_filter( 'menu_order', 'remove_those_menu_items' );

The above is what actually allows you to remove the custom post types from the admin menu system. You just add as many $key as you need to. Instead of me going into detail on what the above means you can read from the original here.

share|improve this answer
    
Beware of this when using Woocommerce plugin as it overrides the option to filter menue! A fixed it by adding add_filter( 'custom_menu_order' , '__return_true', 999); –  numediaweb Jul 20 at 21:41

i don't know whats wrong with the code but whenever i need to limit menu items to users i use: Admin Menu Editor a plugin that does just that for you.

share|improve this answer
    
Thanks for the response but that doesn't really do what I need to do. It'll let you move menu items and everything but wont let you hide menu items for none administrator users. Plus i'd prefer not to use a plugin for this. Thanks –  jetDL Jan 20 '11 at 10:33
    
@Bainternet I usually use Admin Menu Editor as well, but sometimes it just fails to recognize the capability limitations I set up. –  user11577 Jan 5 '12 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.