首页 > 解决方案 > 在自定义用户角色中添加、编辑、删除页面功能

问题描述

尝试在 Wordpress 自定义用户角色中添加“添加、编辑和删除页面”的功能。

我正在下面创建一个自定义用户角色,即“子管理员”。我正在尝试授予对所有“页面”功能的访问权限;但即使确定以下内容,它也不起作用。(没有'添加页面,编辑当前页面选项卡显示)。

也可能要注意;我正在从自定义子主题的/function.php文件中尝试此操作。该角色在以下代码(即子管理员)之后显示在 WP 仪表板中,但是我没有成功允许访问页面。

add_role(
    'sub_admin',
    __( 'Sub Admin' ),
    array(
        'read'         => true,  
        'edit_posts'   => true,
        'publish_posts' => true,
        'edit_pages'   => true,
        'edit_others_pages' => true,
        'publish_page' => true,
        'edit_pages'=>true,
        'edit_published_pages'=>true,
        'publish_pages'=>true,
        'delete_pages'=>true,
        'delete_others_pages'=>true,
        'delete_published_pages'=>true,
    )
);

标签: phpwordpress

解决方案


请尝试通过尝试这些方法来解决您的问题

请确保您的角色是通过获取结果创建的add_role

$result = add_role(/*your args*/);
if ( null !== $result ) {
  echo 'Yay! New role created!';
}
else {
      echo 'Oh... the basic_contributor role already exists.';
}

如果不行。请确保,当您尝试编辑页面时,您具有此 sub_admin 角色

如果问题仍然存在,请注意 wordpress 开发者指南通知

在第一次调用 add_role() 之后,角色和它的能力将被存储在数据库中!

顺序调用不会做任何事情:包括更改功能列表,这可能不是您所期望的行为。

也许你需要remove_role()然后再add_role一次。也许您第一次创建角色时没有某些能力。

另外,请尝试使用 init 操作来添加角色

function wporg_simple_role()
{
    remove_role('sub_admin');
    // or add_role('sub_admin');
}


add_action('init', 'wporg_simple_role');

也许没有编辑角色,所有的管理员能力是

activate_plugins
delete_others_pages
delete_others_posts
delete_pages
delete_posts
delete_private_pages
delete_private_posts
delete_published_pages
delete_published_posts
edit_dashboard
edit_others_pages
edit_others_posts
edit_pages
edit_posts
edit_private_pages
edit_private_posts
edit_published_pages
edit_published_posts
edit_theme_options
export
import
list_users
manage_categories
manage_links
manage_options
moderate_comments
promote_users
publish_pages
publish_posts
read_private_pages
read_private_posts
read
remove_users
switch_themes
upload_files
customize
delete_site

也许管理栏也取决于manage_options。您可以通过检查不是管理页面来删除 manage_options 功能

function change_role() {
    global $wp_roles;

    if ( ! is_admin() ) {
        return;
    }

    // if not admin page - you could temporary remove manage capabilities from 
    // sub_admin role


}

add_action('wp', 'change_role');

推荐阅读