WordPress:在自定义帖子类型上禁用"添加新"

Sta*_*erg 60 wordpress

有没有办法禁用在WordPress(3.0)的自定义帖子类型下添加新帖子的选项?我查看了标签和参数,但找不到任何类似于这种功能的东西.

小智 111

有一个meta功能create_posts没有记录,但在插入各种"添加新"按钮和链接之前,WordPress会使用它来检查.在您的自定义帖子类型声明中,添加capabilities(不要混淆cap),然后将其设置false为如下.

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
Run Code Online (Sandbox Code Playgroud)

  • 您可以将其设置为"管理员"等其他功能,而不是将"create_posts"设置为"false",只允许某些角色创建帖子. (6认同)
  • 这也将删除read/edit/trash功能.用户只能看到帖子列表但不能打开或编辑任何内容.要允许用户编辑现有帖子,您还必须添加选项''map_meta_cap'=> true` (4认同)
  • 我不得不将代码段更改为''create_posts'=>'do_not_allow'以[使其适用于多站点设置](https://github.com/WordPress/WordPress/blob/master/wp-includes/capabilities.php #L1046) - 但是仍然感谢您的见解! (4认同)
  • 'create_posts'=> false在WP4.5中不再有效.我通过使用'create_posts'=>'do_not_allow'(一种不存在的功能)代替@ seamus-leahy来解决这个问题,或许你想更新你的答案? (4认同)
  • 非常适合管理员使用。我需要我的订阅者能够编辑他们现有的帖子,但是即使从查看帖子的页面上,它也会将其禁用。有什么办法吗? (2认同)

The*_*dic 69

完全归功于Seamus Leahy

有一个meta功能create_posts没有记录,但在插入各种"添加新"按钮和链接之前,WordPress会使用它来检查.在您的自定义帖子类型声明中,添加capabilities(不要混淆cap),然后将其设置false为如下.

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // false < WP 4.5, credit @Ewout
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
Run Code Online (Sandbox Code Playgroud)

我可以问你为什么要这样做?

我首先建议更改自定义帖子类型的功能,但我认为没有一个限制谁可以添加帖子,但只有谁可以编辑或发布它们.

它看起来有点脏,但你可以尝试在map_meta_cap全局范围内取消设置项目;

function hide_add_new_custom_type()
{
    global $submenu;
    // replace my_type with the name of your post type
    unset($submenu['edit.php?post_type=my_type'][10]);
}
add_action('admin_menu', 'hide_add_new_custom_type');
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案可能看起来不错......但它只隐藏菜单项 - 它不会阻止用户输入浏览器地址URL来编辑新帖子.类似的东西:http://some-site.com/wp-admin/post-new.php.我正在处理同样的问题,但还没有找到解决方案. (4认同)
  • 值得注意的是,你*必须*包括`'map_meta_cap'=> true`部分 (4认同)
  • 完整的解决方案是http://wordpress.org/support/topic/remove-add-new-from-custom-post-type-for-authors (2认同)

Kir*_*ard 12

上述解决方案的组合可以隐藏链接(尽管有人可以很容易地直接输入URL).

提到@PavelChernov的解决方案依赖于get_post_type()哪个只有在列表中已有帖子时才有效.如果没有帖子,该功能将不会返回任何内容,并且"添加新"链接将可用.另一种方法:

function disable_new_posts() {
    // Hide sidebar link
    global $submenu;
    unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);

    // Hide link on listing page
    if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
        echo '<style type="text/css">
        #favorite-actions, .add-new-h2, .tablenav { display:none; }
        </style>';
    }
}
add_action('admin_menu', 'disable_new_posts');
Run Code Online (Sandbox Code Playgroud)

编辑:如果有人在自己键入URL,阻止直接访问:https://wordpress.stackexchange.com/a/58292/6003


3pe*_*pe3 7

在wordpress和所有帖子类型中都有功能create_posts.此功能用于多个核心文件:

  1. 可湿性粉剂管理员\编辑外形advanced.php
  2. 可湿性粉剂管理员\ edit.php
  3. 可湿性粉剂管理员\包括\ post.php中
  4. 可湿性粉剂管理员\ menu.php
  5. 可湿性粉剂管理员\后new.php
  6. 可湿性粉剂管理员\压this.php
  7. WP-包括\ ADMIN,bar.php
  8. WP-包括\类-WP-XMLRPC-server.php
  9. WP-包括\ post.php中

因此,如果您真的想要禁用此feautere,则必须按角色和每个帖子类型执行此操作.我使用伟大的插件" 用户角色编辑器 "来管理每个角色的功能.

但是功能create_posts怎么样?那么这个功能没有被映射,并且create_posts也等于create_posts所以我们应该修复它并映射每个帖子类型的能力.

所以你可以在functions.php中添加这段代码,然后就可以管理这个功能了.

function fix_capability_create(){
    $post_types = get_post_types( array(),'objects' );
    foreach ( $post_types as $post_type ) {
        $cap = "create_".$post_type->name;
        $post_type->cap->create_posts = $cap;
        map_meta_cap( $cap, 1); 
    }
}
add_action( 'init', 'fix_capability_create',100);
Run Code Online (Sandbox Code Playgroud)

所以这里我们没有隐藏或删除菜单元素......这里我们正在删除用户的功能(包括xmlrpc请求).

操作是init而不是admin_init或其他任何因为init优先级100阻止在管理栏,侧栏等(在所有wp界面中)显示"添加新".


小智 5

add_action("load-post-new.php", 'block_post');

function block_post()
{
    if($_GET["post_type"] == "custom_type") 
        wp_redirect("edit.php?post_type=custom_type");
}
Run Code Online (Sandbox Code Playgroud)


Wil*_*aig 5

WordPress 网络:我发现如果您以网络的超级管理员身份登录Seamus Leahy 的答案将不起作用,如果用户没有能力,映射或其他方式,当 current_user_can($cap ) 由 CMS 调用。通过深入研究核心,我发现您可以执行以下操作。

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
  ),
  'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));
Run Code Online (Sandbox Code Playgroud)

接受的答案隐藏菜单项,但页面仍然可以访问。


l2a*_*lba 5

对于挂号邮寄类型禁用创建新帖子:(例如对于postpage

function disable_create_newpost() {
    global $wp_post_types;
    $wp_post_types['post']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['page']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');
Run Code Online (Sandbox Code Playgroud)