0

I want to disable the default post types for user author in the wp-admin panel of the wordpress and display only custom post types.

For that i had got the code given below.

function remove_menus(){

$author = wp_get_current_user();
if(isset($author->roles[0])){ 
    $current_role = $author->roles[0];
}else{
    $current_role = 'no_role';
}

if($current_role == 'author'){ 

  remove_menu_page( 'index.php' );                  //Dashboard
  remove_menu_page( 'edit.php' );                   //Posts
  remove_menu_page( 'upload.php' );                 //Media
  remove_menu_page( 'edit.php?post_type=page' );    //Pages
  remove_menu_page( 'edit-comments.php' );          //Comments
  remove_menu_page( 'themes.php' );                 //Appearance
  remove_menu_page( 'plugins.php' );                //Plugins
  remove_menu_page( 'users.php' );                  //Users
  remove_menu_page( 'tools.php' );                  //Tools
  remove_menu_page( 'options-general.php' );        //Settings
}
}
add_action( 'admin_menu', 'remove_menus' ); 

The default post types disappear from the admin panel but the problem with the above code is that the pages are still accessible when the url is typed in and when loading admin panel from the top Dashboard appears.

I want the default post types blocked or page should not be loaded whereas custom post type page should load.

Thought the post type is disabled the pages load on type the url in address bar.

enter image description here enter image description here

2
  • For this you need to change the required $capability, so that if an Author (or below) tries to view the page they are told they do not have access.
    – David Gard
    Commented Feb 18, 2015 at 13:40
  • could you guide me how to do that Commented Feb 18, 2015 at 13:44

1 Answer 1

0

Add this to your functions.php file, and note that you'll need to grab and edit the capabilities for each post type (Post, Page and Attachment) and taxonomy (Category and Post Tag).

You should note that there are more capabilities than those listed, but I don't believe that the need changing. Therefore I advise you to output the default capabilites for each post type and work out exactly which ones you need to change, and what they should be changed to.

I haven't looked to much in to exactly what permission you would need to set, but I believe that this example will make it so that only Editors and above can do anything with Posts.

For more information on Roles and Capabilities, check out the Codex.

add_action('init', 'my_change_post_object_cap', 1);
function my_change_post_object_cap(){

    $post = get_post_type_object('post');
    $post_cap = &$post->cap;

    $post_cap->edit_post                    = 'edit_others_posts';
    $post_cap->read_post                    = 'edit_others_posts';
    $post_cap->delete_post                  = 'delete_others_posts';
    $post_cap->edit_posts                   = 'edit_others_posts';
    $post_cap->edit_others_posts            = 'edit_others_posts';
    $post_cap->publish_posts                = 'edit_others_posts';
    $post_cap->read                         = 'edit_others_posts';
    $post_cap->delete_posts                 = 'delete_others_posts';
    $post_cap->delete_published_posts       = 'delete_others_posts';
    $post_cap->edit_published_posts         = 'edit_others_posts';
    $post_cap->create_posts                 = 'edit_others_posts';

    $page = get_post_type_object('page');
    $page_cap = &$page->cap;
    /** do the same as above, but for pages capabilities */

    $attachment = get_post_type_object('attachment');
    $attachment_cap = &$attachment->cap;
    /** do the same as above, but for attachment capabilities */

    $category = get_taxonomy('category');
    $category_cap = $category->cap;
    /** do the same as above, but for category capabilities */

    $post_tag = get_taxonomy('post_tag');
    $post_tag_cap = $post_tag->cap;
    /** do the same as above, but for post_tag capabilities */

}
4
  • thnaks it worked.....But i want to do this parallely for upload.php ,edit-comments.php Commented Feb 18, 2015 at 15:10
  • i worked for edit.php and edit.php?post_type=page Commented Feb 18, 2015 at 15:11
  • please help me with this Commented Feb 19, 2015 at 6:35
  • i want it to work edit-comments.php and upload.php too but i want that "image upload pop up comming out" not to be blocked Commented Feb 19, 2015 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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