允许编辑者编辑菜单?
7 个回答
- 投票数
-
- 2011-02-24
将此添加到主题的
functions.php
:// add editor the privilege to edit theme // get the the role object $role_object = get_role( 'editor' ); // add $cap capability to this role object $role_object->add_cap( 'edit_theme_options' );
add this to your theme's
functions.php
:// add editor the privilege to edit theme // get the the role object $role_object = get_role( 'editor' ); // add $cap capability to this role object $role_object->add_cap( 'edit_theme_options' );
-
get_role是一堂课吗?is get_role a class?
- 1
- 2011-02-24
- Mild Fuzz
-
@Mild Fuzz-本身不是,但它返回`WP_Role`的实例@Mild Fuzz - not itself no, but it returns an instance of `WP_Role`
- 4
- 2011-08-14
- TheDeadMedic
-
据我所知,您可能不应该对每个请求都执行此操作,因为这会导致数据库写入.仅在`admin_init`和`!$ role_object-> has_cap('edit_theme_options')You probably shouldn't do this on every request, as this causes a db write as far as i understood. Better on `admin_init` and only `if !$role_object->has_cap('edit_theme_options')`
- 9
- 2017-02-14
- jsphpl
-
此设置已保存到数据库(在表wp_options中的字段wp_user_roles中),因此最好在主题/插件激活时运行此设置.参见https://codex.wordpress.org/Function_Reference/add_capThis setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation. See https://codex.wordpress.org/Function_Reference/add_cap
- 0
- 2018-03-12
- Pim Schaaf
-
或者您可以将其添加到functions.php中,运行一次,然后将其删除Or you could add it to functions.php, run it once and then remove it
- 0
- 2019-01-16
- d79
-
- 2013-04-16
编辑: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');
hide_menu()
函数中的最后3行是针对我的主题的主题.您可以通过在管理面板中单击要隐藏的子菜单来找到第二个参数.然后,您的URL将类似于:example.com/wp-admin/themes.php?page=yiw_panel因此,在此示例中,
remove_submenu_page()
函数的第二个参数将是yiw_panel
EDIT: update for WP 4.9 & only hiding menu items for Editor
If you want your users to be able to change the navigation menu, but not the other options under appearance: use this
// Do this only once. Can go anywhere inside your functions.php file $role_object = get_role( 'editor' ); $role_object->add_cap( 'edit_theme_options' );
You can comment out this entire code after you have refreshed your admin panel, because the above code will make persistent changes to the database.
You now have all the options under appearance visible to the editors. You can hide the other options like so:
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');
The last 3 lines in the
hide_menu()
function are theme specific for my theme. You can find the second parameter by clicking on the submenu you want to hide, in the admin panel. Your URL will then be something like: example.com/wp-admin/themes.php?page=yiw_panelSo, in this example, the second parameter for the
remove_submenu_page()
function will beyiw_panel
-
这也为管理员隐藏了主题等.this hides themes etc for admins too.
- 1
- 2017-09-15
- JorgeLuisBorges
-
- 2014-01-09
在WordPress 3.8中,这比当前接受的答案更好.
/** * @var $roleObject WP_Role */ $roleObject = get_role( 'editor' ); if (!$roleObject->has_cap( 'edit_theme_options' ) ) { $roleObject->add_cap( 'edit_theme_options' ); }
In WordPress 3.8, this would be better code than the current accepted answer.
/** * @var $roleObject WP_Role */ $roleObject = get_role( 'editor' ); if (!$roleObject->has_cap( 'edit_theme_options' ) ) { $roleObject->add_cap( 'edit_theme_options' ); }
-
- 2010-11-17
当我查看管理菜单结构时,似乎
nav-menus.php
链接与edit_theme_options
功能相关.您可以修改编辑者角色以包括此功能吗?这也会为他们提供编辑窗口小部件的选项,我不知道这是否有问题吗?所有菜单Ajax内容都受此功能限制,因此仅更改管理菜单功能以编辑菜单将可能行不通.When I look at the admin menu structure, it seems the
nav-menus.php
link is tied to the capabilityedit_theme_options
. Can you modify the editor role to include this capability? This would also give them the option to edit widgets, I don't know whether this is a problem? All the menu Ajax stuff is restricted by this capability, so just changing the admin menu capability for editing menus will probably not work. -
-
- 2011-01-05
我发现,您的菜单将以这种方式工作:安装插件" 用户角色编辑器",您还可以在其中编辑编辑者角色等的条件.打开edit_theme_options.但是现在:您将在"主题","小部件"下看到"菜单"-选项. 对我来说:单击"菜单"(作为编辑器)后,我看不到填充选项,而是空白.因此,我将停用插件"用户角色编辑器",并正确显示"菜单"的填充选项.请注意,停用插件"用户角色编辑器"仍然是激活编辑器的条件!对我有好处,也许对您也有帮助
I´ve found, that your menu will work this way: instal plugin "User Role Editor" and there you can edit condition for editor role and other too. Switch edit_theme_options ON. But now: you will see "menu" -option under "themes", "widgets". For me: After click to "menu" (as editor) I´d not see filled options but empty. So I´d deactivate plugin "User Role Editor" and filled options for "menu" appears correctly. Note that deactivating plugin "User Role Editor" remains conditions for editor active! Good for me, maybe it will help you too
-
- 2020-03-30
现在是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'); } } } }
It's 2020, WordPress is now past V5.3 so i thought i would contribute an updated version in which the setting is saved only once in the database - upon theme activation, and removed when the theme is desactivated.
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' );
I also prefer to code in an OOP style so this is an OOP version:
/** * 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'); } } } }
我希望能够授予我的编辑者更改菜单的权力,可以这样做吗?
外观标签似乎根本不是一个选项,我可以这样做吗?