【发布时间】:2019-10-02 17:15:49
【问题描述】:
有什么方法可以禁用在 WordPress (3.0) 中的自定义帖子类型下添加新帖子的选项?我查看了标签和参数,但找不到任何类似于此类功能的内容。
【问题讨论】:
-
谈论挖掘死者 - 我今天跌跌撞撞地回到这里,我可以看到 Seamus 的答案,您应该考虑将其标记为已接受,这绝对是“正确”的答案。
标签: wordpress
有什么方法可以禁用在 WordPress (3.0) 中的自定义帖子类型下添加新帖子的选项?我查看了标签和参数,但找不到任何类似于此类功能的内容。
【问题讨论】:
标签: wordpress
有一个元功能create_posts 即documented here,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
));
您可能还想将map_meta_cap 设置为true。没有它,您将无法再访问帖子的编辑页面。
【讨论】:
'map_meta_cap' => true
create_posts 设置为false,您还可以将其设置为其他功能,例如administrator,以仅允许某些角色创建帖子。
'create_posts' => 'do_not_allow' 到 make it work for multisite setups - 但仍然感谢您的见解!
上述解决方案的组合可以隐藏链接(尽管有人可以很容易地直接输入 URL。
solution mentioned @3pepe3 依赖于 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');
编辑:如果有人自己键入 URL,以防止直接访问:https://wordpress.stackexchange.com/a/58292/6003
【讨论】:
在 wordpress 中,对于所有帖子类型,都有 create_posts 功能。此功能用于多个核心文件:
因此,如果您真的想禁用此功能,则必须按角色和帖子类型执行此操作。 我使用出色的插件“User Role Editor”来管理每个角色的功能。
但是 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);
所以这里我们不是隐藏或删除菜单元素...这里我们是删除用户的功能(包括 xmlrpc 请求)。
该操作是 init 而不是 admin_init 或其他任何东西,因为优先级为 100 的 init 会阻止在管理栏、侧边栏等(在所有 wp 界面中)显示“添加新”。
【讨论】:
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");
}
【讨论】:
禁止为已注册的帖子类型创建新帖子:(例如 post 和 page)
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');
【讨论】:
WordPress 网络:我发现Seamus Leahy's answer 不起作用 如果您以网络的超级管理员身份登录,则用户没有该功能也没关系,映射或其他方式,当 CMS 调用 current_user_can($cap) 时。通过深入挖掘核心,我发现您可以执行以下操作。
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 隐藏了菜单项,但页面仍然可以访问。
【讨论】:
@斯塔凡·埃斯特伯格,
这是在自定义 posttypes 中隐藏 Add New 或 Create New 按钮的最佳方式
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
它禁止在管理菜单和帖子类型列表上方的自定义帖子类型中创建新帖子。
【讨论】:
我找到了这个最简单的方法。只需将此代码添加到主题的function.php。
function hd_add_buttons() {
global $pagenow;
if (is_admin()) {
if ($_GET['post_type'] == 'custom_post_type_name') {
echo '<style>.add-new-h2{display: none !important;}</style>';
}
}
}
add_action('admin_head', 'hd_add_buttons');
【讨论】:
由于问题是“如何在自定义帖子类型上禁用添加新按钮”,而不是“如何限制用户编辑自定义帖子类型”,我认为答案应该是纯粹用 CSS 隐藏按钮,通过添加这个到functions.php文件:
add_action( 'admin_head', function(){
ob_start(); ?>
<style>
#wp-admin-bar-new-content{
display: none;
}
a.page-title-action{
display: none !important;
}
#menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
display:none;
}
</style>
<?php ob_end_flush();
});
【讨论】: