允许用户发布他们有权编辑的页面的子页

时间:2019-06-26 作者:Anna

我创建了一个名为自定义作者的自定义角色。此角色的用户可以编辑自己的页面并添加新页面。我想限制新页面的创建,并允许自定义作者仅将页面创建为自己页面的子页面。换句话说,我希望他们仍然能够创建新页面,但只能作为他们有编辑器访问权限的页面的子页面。

Here is what I think I need to do: 删除功能publish_pages (我可以通过插件来实现)并通过以下操作添加新功能:

function add_theme_custom_author_caps() {
    $role = get_role( \'custom-author\' );
    $role->add_cap( \'publish_own_pages\' ); 
}
add_action( \'admin_init\', \'add_theme_custom_author_caps\');
这里的问题是add_cap( \'publish_own_pages\' ) 这根本行不通,我很难理解如何根据自己的需求创建新功能。

1 个回复
最合适的回答,由SO网友:WebElaine 整理而成

自定义帖子类型将更容易设置,并且您不必编写自己的解决方案来在编辑器中检查当前用户是否有创建新页面或编辑内容的权限。

您可以在任意URL上设置CPT存档。

因此,在您的示例中,您可以在/animals/ 和常规子页面/animals/carnivores/. 您的自定义角色将无权编辑这些角色。

然后,您将创建“cat”的CPT。您应该查看注册CPT的所有选项,因为您可能不需要所有选项,但有些选项可能很方便,如“menu\\u icon”,它设置出现在wp admin左侧边栏中的图标,还有一些选项是必需的,如“rewrite”,它是您需要在所需URL上设置它们的,以及“capabilities”,它是您将用来限制谁可以访问什么。您还需要决定在“支持”中添加什么内容-此示例包括标题、编辑器和缩略图(特色图像)。

<?php
/* Plugin Name: WPSE custom post types
*/
// on init, create a custom post type called Cat
add_action(\'init\', \'wpse_create_post_types\');
function wpse_create_post_types() {
    // Capabilities: this is how you\'ll enable some users to edit, delete, etc.
    $capabilities = array(
        \'edit_post\'                 => \'edit_cat\',
        \'read_post\'                 => \'read_cat\',
        \'delete_post\'               => \'delete_cat\',
        \'create_posts\'              => \'create_cats\',
        \'delete_posts\'              => \'delete_cats\',
        \'delete_others_posts\'       => \'delete_others_cats\',
        \'delete_private_posts\'      => \'delete_private_cats\',
        \'delete_published_posts\'    => \'delete_published_cats\',
        \'edit_posts\'                => \'edit_cats\',
        \'edit_others_posts\'         => \'edit_others_cats\',
        \'edit_private_posts\'        => \'edit_private_cats\',
        \'edit_published_posts\'      => \'edit_published_cats\',
        \'publish_posts\'             => \'publish_news\',
        \'read_private_posts\'        => \'read_private_news\'
    );
    // Other CPT arguments
    $args = array(
        // Important: make sure to include the next 2 args for capabilities
        \'map_meta_cap\' => true,
        \'capabilities\' => $capabilities,
        // Important: make sure to set the rewrite to your desired archive URL
        \'rewrite\'           => array(\'slug\' => \'animals/carnivores/cats\'),
        // You can adjust the other args as needed
        \'public\' => true,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\')
    );
    // Actually create the post type
    register_post_type(\'cat\', $args);
}
因为您已经创建了新功能,所以还没有用户可以访问它,包括管理员。因此,要授予访问权限,请将其添加到相同的插件文件中:

// \'cat_author\' is a slug, \'Custom Author Cats\' is how you will see this role in wp-admin
add_role(\'cat_author\', \'Custom Author Cats\', array(
    // You may or may not want to give them all these capabilities.
    // For example you could let them publish but not delete.
    \'delete_cats\' => true,
    \'create_cats\' => true,
    \'delete_cats\' => true,
    \'delete_others_cats\' => true,
    \'delete_private_cats\' => true,
    \'delete_published_cats\' => true,
    \'edit_cats\' => true,
    \'edit_others_cats\' => true,
    \'edit_private_cats\' => true,
    \'edit_published_cats\' => true,
    \'publish_cats\' => true,
    \'read_private_cats\' => true,
    // If you want them to be able to upload files and read on the front end,
    // make sure to include the following 2 capabilities:
    \'read\' => true,
    \'upload_files\' => true
));
最后,你可能还想让管理员(也许像你一样)能够添加/编辑/删除这些自定义新闻帖子。如果是,请将以下内容添加到插件:

add_action(\'admin_init\', \'wpse_add_admin_caps\');
function wpse_add_admin_caps() {
    $role = get_role(\'administrator\');
    $role->add_cap(\'delete_cats\');
    $role->add_cap(\'create_cats\');
    $role->add_cap(\'delete_cats\');
    $role->add_cap(\'delete_others_cats\');
    $role->add_cap(\'delete_private_cats\');
    $role->add_cap(\'delete_published_cats\');
    $role->add_cap(\'edit_cats\');
    $role->add_cap(\'edit_others_cats\');
    $role->add_cap(\'edit_private_cats\');
    $role->add_cap(\'edit_published_cats\');
    $role->add_cap(\'publish_cats\');
    $role->add_cap(\'read_private_cats\');
}
您需要对每个自定义帖子类型重复此操作,并确保在插件激活后,访问设置>永久链接页面。您不必更改任何设置,但访问它将刷新重写规则并设置插件中定义的新URL。然后,最后一步是添加每个用户并为其分配正确的角色。

相关推荐

Missing Author Information

我在单曲上显示作者姓名和头像时遇到问题。php页面。立柱装载良好,所有正确信息可用。然而,在哪里找不到头像和名字。我不确定我做错了什么。非常感谢您的帮助<div class=\"row\"> <div> <h1 class=\"primary-color\"> <?php the_title(); ?></h1> <span class=\"avatar\">