禁用wp-admin中的默认帖子(帖子、页面、评论和媒体)

时间:2015-02-18 作者:Shahrukh Khan

我想在wordpress的wp admin面板中禁用user author的默认帖子类型,并只显示自定义帖子类型。

为此,我得到了下面给出的代码。

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\' ); 
默认的帖子类型从管理面板中消失,但上面代码的问题是,当键入url并从顶部仪表板加载管理面板时,页面仍然可以访问。

我想阻止默认的帖子类型,或者不应该加载页面,而应该加载自定义帖子类型页面。

虽然帖子类型已禁用,但页面加载时在地址栏中键入url。

enter image description hereenter image description here

1 个回复
最合适的回答,由SO网友:David Gard 整理而成

将此添加到functions.php 请注意,您需要获取和编辑每个帖子类型(帖子、页面和附件)和分类法(类别和帖子标记)的功能。

您应该注意到,有比列出的功能更多的功能,但我不认为需要改变。因此,我建议您输出每个帖子类型的默认功能,并确定您需要更改哪些,以及应该更改哪些。

我还没有详细了解您需要设置什么样的权限,但我相信这个示例将使您只需要Editors 以上内容可以对帖子做任何事情。

有关的更多信息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 */

}

结束