有没有办法禁用在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)
The*_*dic 69
有一个meta功能
create_posts没有记录,但在插入各种"添加新"按钮和链接之前,WordPress会使用它来检查.在您的自定义帖子类型声明中,添加capabilities(不要混淆cap),然后将其设置false为如下.Run Code Online (Sandbox Code Playgroud)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全局范围内取消设置项目;
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)
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
在wordpress和所有帖子类型中都有功能create_posts.此功能用于多个核心文件:
因此,如果您真的想要禁用此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)
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)
该接受的答案隐藏菜单项,但页面仍然可以访问。
对于挂号邮寄类型禁用创建新帖子:(例如对于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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41178 次 |
| 最近记录: |