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.
Codex – Register Post Type
See the
capability_type
andcapabilities
arguments forregister_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.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 asmanage_options
.Table of roles and their caps(for quick reference).
http://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table
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:
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.
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.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.