问题描述

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

最佳解决思路

完全信用 Seamus Leahy

There is a meta capability create_posts that is not documented but is used by WordPress to check before inserting the various ‘Add New’ buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.

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 )); 

请问为什么要这样做?

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

它看起来有点脏,但您可以尝试取消设置 $submenu 全局中的项目;

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');

次佳解决思路

有一个元功能 create_posts 没有记录,但是在插入各种’Add New’ 按钮和链接之前,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
));

第三种解决思路

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

提到 @PavelChernov 的解决方案依赖于 get_post_type(),只有在列表中已经有一个帖子才能工作。如果没有帖子,该功能将不会返回任何内容,并且”Add New” 链接将可用。另一种方法:

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');

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

第四种思路

WordPress 网络:我发现如果您以网络的超级管理员身份登录,Seamus Leahy’s answer 不起作用,如果用户没有能力,映射或其他方式,当 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
));

菜单项 accepted answer 隐藏,但页面仍然可以访问。

第五种思路

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");
}

第六种思路

在 wordpress 和所有的帖子类型有 create_posts 的能力。此功能用于几个核心文件:

  1. wp-admin edit-form-advanced.php

  2. wp-admin edit.php

  3. wp-admin 包括 post.php 中

  4. wp-admin menu.php

  5. wp-admin post-new.php

  6. wp-admin press-this.php

  7. wp-includes admin-bar.php

  8. wp-includes class-wp-xmlrpc,server.php

  9. wp-includes post.php 中

所以如果你真的要禁用这个 feautere,你必须按角色和每个帖子类型进行。我使用伟大的插件 “User Role Editor” 来管理每个角色的功能。

但是 create_posts 的能力呢?那么这个能力没有被映射,并且 create_posts 等于 create_posts,所以我们应该修复这个并且映射每个 post 类型的能力。

所以你可以在你的 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);

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

该操作是 init,而不是 admin_init 或其他任何因为 init 优先级为 100,阻止在管理栏,侧边栏等 (在所有 wp 接口中) 显示”add new” 。

第七种思路

@ Staffan Estberg,

这是在自定义邮件列表中隐藏添加新建或创建新按钮的最佳方法

'capability_type'    => 'post',

        'capabilities'       => array( 'create_posts' => false ),

        'map_meta_cap'       => true,

它禁用在管理菜单中以及帖子类型列表之上的自定义帖子类型中创建新帖子。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。