WordPress:在自定义帖子类型上禁用“添加新”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3235257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress: Disable "Add New" on Custom Post Type
提问by Staffan Estberg
Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I've looked into labels and arguments but can't find anything that would resemble such a feature.
有什么方法可以禁用在 WordPress (3.0) 的自定义帖子类型下添加新帖子的选项?我查看了标签和参数,但找不到任何类似于此类功能的内容。
回答by Seamus Leahy
There is a meta capability create_posts
that is documented hereand 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.
此处记录了一个元功能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
));
You'll probably want to set map_meta_cap
to true
as well. Without it, you won't be able to access the posts' editing pages anymore.
你可能会想设定map_meta_cap
到true
为好。没有它,您将无法再访问帖子的编辑页面。
回答by TheDeadMedic
Full credit to Seamus Leahy
完全归功于Seamus Leahy
There is a meta capability
create_posts
that is documented hereand is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, addcapabilities
(not to be confused withcap
) and then set it tofalse
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 ));
You'll probably want to set
map_meta_cap
totrue
as well. Without it, you won't be able to access the posts' editing pages anymore.
此处记录了一个元功能
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 ));
你可能会想设定
map_meta_cap
到true
为好。没有它,您将无法再访问帖子的编辑页面。
May I ask why you want to do this?
我可以问你为什么要这样做吗?
I would at first have suggested changing the capabilities for your custom post type, but I don't think there's one that limits who can addposts, but only who can edit or publishthem.
我一开始会建议更改自定义帖子类型的功能,但我认为没有限制谁可以添加帖子,而只有谁可以编辑或发布帖子。
It looks a little dirty, but you could try unsetting the item in the $submenu
global;
它看起来有点脏,但您可以尝试在$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');
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');
回答by Kirk Beard
The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.
上述解决方案的组合可以隐藏链接(尽管有人可以很容易地直接输入 URL。
The solution mentioned @3pepe3relies on get_post_type()
which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:
@3pepe3 提到的解决方案依赖于get_post_type()
which 仅在列表中已有帖子时才有效。如果没有帖子,该函数将不会返回任何内容,并且“添加新”链接将可用。另一种方法:
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');
EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003
编辑:如果有人在自己输入 URL 时防止直接访问:https: //wordpress.stackexchange.com/a/58292/6003
回答by 3pepe3
In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :
在 wordpress 和所有帖子类型中,都有 create_posts 功能。此功能用于几个核心文件:
- wp-admin\edit-form-advanced.php
- wp-admin\edit.php
- wp-admin\includes\post.php
- wp-admin\menu.php
- wp-admin\post-new.php
- wp-admin\press-this.php
- wp-includes\admin-bar.php
- wp-includes\class-wp-xmlrpc-server.php
- wp-includes\post.php
- wp-admin\edit-form-advanced.php
- wp-admin\edit.php
- wp-admin\includes\post.php
- wp-admin\menu.php
- wp-admin\post-new.php
- wp-admin\press-this.php
- wp-includes\admin-bar.php
- wp-includes\class-wp-xmlrpc-server.php
- wp-includes\post.php
So if you really want to disable this feautere you must do it per role and per post type. I use the great plugin "User Role Editor" to manage the capabilities per role.
因此,如果您真的想禁用此功能,则必须按角色和每个帖子类型执行此操作。我使用很棒的插件“用户角色编辑器”来管理每个角色的功能。
But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.
但是 create_posts 功能呢?好吧,此功能未映射,并且 create_posts 也等于 create_posts,因此我们应该解决此问题并映射每个帖子类型的功能。
So you can add this piece of code in your functions.php and the you can manage this capability.
所以你可以在你的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);
So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).
所以在这里我们没有隐藏或删除菜单元素……在这里我们删除了用户的功能(包括 xmlrpc 请求)。
The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).
该操作是 init 而不是 admin_init 或其他任何内容,因为优先级为 100 的 init 阻止在管理栏、侧边栏等(在所有 wp 界面中)显示“添加新”。
回答by Will Craig
WordPress Networks: I found that Seamus Leahy's answerdoesn't workif you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.
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
));
The accepted answerhides the menu item, but the page is still accessible.
该接受的答案隐藏菜单项,但页面仍然可以访问。
回答by andrea
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");
}
回答by l2aelba
Disable creating new post for registered post-types:(example for post
and page
)
禁止为已注册的帖子类型创建新帖子:(例如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');
回答by clap
@ Staffan Estberg,
@斯塔凡埃斯特伯格,
This is best way to hide the Add New or Create New button in custom postypes
这是在自定义 postypes 中隐藏 Add New 或 Create New 按钮的最佳方法
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
It disable to create new post in custom post types both side in admin menu and above the list of post type.
它禁止在管理菜单两侧和帖子类型列表上方以自定义帖子类型创建新帖子。
回答by Umair Hamid
I found this simplest way for this. Just ad this code into theme's function.php
.
我为此找到了这个最简单的方法。只需将此代码添加到主题的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');
回答by rafa226
As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :
由于问题是“如何在自定义帖子类型上禁用添加新按钮”,而不是“如何限制用户编辑自定义帖子类型”,因此我认为答案应该纯粹是用 css 隐藏按钮,将其添加到函数.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();
});