allow editors to edit menus?

时间:2010-11-17 作者:Mild Fuzz

我希望能够授予我的编辑更改菜单的权力,这可以做到吗?

“外观”选项卡似乎根本不是一个选项,我可以这样做吗?

4 个回复
SO网友:Jules

编辑:更新WP 4.9和;仅隐藏编辑器的菜单项

如果希望用户能够更改导航菜单,但不能更改外观下的其他选项:请使用

// Do this only once. Can go anywhere inside your functions.php file
$role_object = get_role( \'editor\' );
$role_object->add_cap( \'edit_theme_options\' );
您可以在刷新管理面板后注释掉整个代码,因为上面的代码将对数据库进行持久更改。

现在,编辑器可以看到“外观”下的所有选项。您可以像这样隐藏其他选项:

function hide_menu() {

    if (current_user_can(\'editor\')) {

        remove_submenu_page( \'themes.php\', \'themes.php\' ); // hide the theme selection submenu
        remove_submenu_page( \'themes.php\', \'widgets.php\' ); // hide the widgets submenu
        remove_submenu_page( \'themes.php\', \'customize.php?return=%2Fwp-admin%2Ftools.php\' ); // hide the customizer submenu
        remove_submenu_page( \'themes.php\', \'customize.php?return=%2Fwp-admin%2Ftools.php&autofocus%5Bcontrol%5D=background_image\' ); // hide the background submenu


        // these are theme-specific. Can have other names or simply not exist in your current theme.
        remove_submenu_page( \'themes.php\', \'yiw_panel\' );
        remove_submenu_page( \'themes.php\', \'custom-header\' );
        remove_submenu_page( \'themes.php\', \'custom-background\' );
    }
}

add_action(\'admin_head\', \'hide_menu\');
中的最后3行hide_menu() 函数是特定于我的主题的主题。通过单击管理面板中要隐藏的子菜单,可以找到第二个参数。然后,您的URL将类似于:example。com/wp管理员/主题。php?第页=yiw_panel

因此,在本例中remove_submenu_page() 功能将是yiw_panel

SO网友:BenFreke

在WordPress 3.8中,这将是比当前公认答案更好的代码。

/**
 * @var $roleObject WP_Role
 */
$roleObject = get_role( \'editor\' );
if (!$roleObject->has_cap( \'edit_theme_options\' ) ) {
    $roleObject->add_cap( \'edit_theme_options\' );
}

SO网友:Jan Fabry

当我查看管理菜单结构时,似乎the nav-menus.php link is tied to the capability edit_theme_options. 能否修改编辑器角色以包含此功能?这也会give them the option to edit widgets, 我不知道这是否是个问题?所有的菜单Ajax都受到这个功能的限制,所以仅仅更改管理菜单的功能来编辑菜单可能是行不通的。

SO网友:pixeline

现在是2020年,WordPress已经超过了V5。3所以我想我会提供一个更新版本,其中设置只在数据库中保存一次-在主题激活时保存,在主题取消激活时删除。

function add_theme_caps(){
  global $pagenow;
  // gets the author role
  $role = get_role(\'editor\');

  if (\'themes.php\' == $pagenow && isset($_GET[\'activated\'])) { 
          // Test if theme is activated
          // Theme is activated
          // This only works, because it accesses the class instance.
          // would allow the editor to edit the theme options
          $role->add_cap(\'edit_theme_options\');
  } else {
         // Theme is deactivated
         // Remove the capability when theme is deactivated
         $role->remove_cap(\'edit_theme_options\');
  }
}
add_action( \'load-themes.php\', \'add_theme_caps\' );

我还喜欢以OOP风格编写代码,因此这是一个OOP版本:


/**
 * YourClient Class
 *
 * @author   John Doe
 * @package  YourClient
 * @since    1.0
 */
if (!defined(\'ABSPATH\')) {
    exit;
}
if (!class_exists(\'YourClient\')) {
    /**
     * The main YourClient class
     */
    class YourClient
    {
        /**
         * Setup class
         */

        public function __construct()
        {
            // Give more privileges on Theme Activation
            add_action(\'load-themes.php\', array($this, \'add_theme_caps\'));
        }

        function add_theme_caps()
        {
            global $pagenow;

            // gets the author role
            $role = get_role(\'editor\');

            if (\'themes.php\' == $pagenow && isset($_GET[\'activated\'])) { // Test if theme is activated
                // Theme is activated

                // This only works, because it accesses the class instance.
                // would allow the author to edit others\' posts for current theme only
                $role->add_cap(\'edit_theme_options\');
            } else {
                // Theme is deactivated
                // Remove the capability when theme is deactivated
                $role->remove_cap(\'edit_theme_options\');
            }
        }
    }
}

结束

相关推荐