jQuery Solves: How to allow Editors to edit Menus (only!)
-
PREFACE:
I previously posted this, but while that version of the code worked, it broke the rules of having CSS styles NOT in the < head > section. So I reworked it to use jQuery instead, so there is no issue or problems with standards validation. I hope!THE NEW AND IMPROVED VERSION USING jQUERY:
A jQuery Solution for non-Admins (ie, Editor) to have access to APPEARANCE Menus and ONLY Menus (or more!). Admin’s or those roles given ‘edit_theme_options’ capabilities, have access to the Appearance sub-menus for Themes, Widgets, Menus, and Background. This CSS solution allows you to hide one or more of the sub-menus, preventing unwanted access to the options you don’t want Users to have and allowing access to those you do.I’m going to present the jQuery code piece that does the job first, so you don’t have to read the What, Why and How here. I’ll put the info and write up about it after the code.
THE CODE:
<?php // Using jQuery: How to allow Editors to edit only Menus (or more!) [by Chris Lemke aka PrettySickPuppy|gmail] if ( is_user_logged_in() ) { // This IF may be redundant, but safe is better than sorry... if ( current_user_can('edit_theme_options') && !current_user_can('manage_options') ) { // Check if non-Admin ?> <script> jQuery.noConflict(); jQuery(document).ready(function() { // Comment out the line you WANT to enable, so it displays (is NOT removed). // For example, the jQuery line for MENUS is commented out below so it's not removed. // THEMES: If you want to allow THEMES, also comment out APPEARANCE if you want it to display Themes when clicked. (Default behaviour) jQuery('li#menu-appearance.wp-has-submenu li a[href="themes.php"]').remove(); jQuery('li#menu-appearance.wp-has-submenu a.wp-has-submenu').removeAttr("href"); // WIDGETS: jQuery('li#menu-appearance.wp-has-submenu li a[href="widgets.php"]').remove(); // MENUS: // jQuery('li#menu-appearance.wp-has-submenu li a[href="nav-menus.php"]').remove(); // BACKGROUND: jQuery('li#menu-appearance.wp-has-submenu li a[href="themes.php?page=custom-background"]').remove(); }); </script> <?php } // End IF current_user_can... } // End IF is_user_logged_in... ?>
MORE INFORMATION:
Place the PHP/jQuery code snippet in your admin-footer.php file, JUST BEFORE the ending </body> element tag. The admin-footer.php file is located in the wp-admin directory.To choose which Appearance sub-menu(s) you want to allow access to, simply comment out the jQuery line for that option or menu item. To remove or deny access to an option/menu item, leave it active (not commented out). The code snippet has Help Comments that explains this as well.
ABOUT THE CODE:
The IF statements are used to check and see if the logged in user is an Admin or non-Admin user. True Admins should only have access to the “manage_options” capability and all other users or roles should not.If a user is not an Admin BUT HAS the ‘edit_theme_options’ capability added to their assigned role (ie, Editor), only then will the jQuery statements will be processed to either Remove or Keep the options you want them to have based on whether or not which jQuery lines have been commented out or not. It’s that simple.
AN IMPORTANT POINT:
For this jQuery Solution to have any affect on what sub-menus are displayed, the ‘edit_theme_options’ capability must be given to the role (or roles) that need or want to have access to the Appearance sub-menus (for Themes, Widgets, Menus, and Backrounds).There are various plugins available that can modify or extend roles, or you can do it yourself by editing another file and adding the proper code to it. For your convenience, I will add how to do this here if you don’t want another plugin to install, or just to test this jQuery Solution first…
Edit and add the follwing code snippet to the functions.php file in your WordPress Theme. Put the code at the end of the functions.php file, so it’s not nested by accident in any logic statements or functions.
CODE FOR ASSIGNING EDITOR ROLE THE ‘edit_theme_options’ CAPABILITY:<?php // get the the role object - editor, author, etc. (or any specially created role) $role = get_role('editor'); // add a capability to this role object - The 'edit_theme_options' enables Dashboard APPEARANCE and Sub-Menus of Themes, Widgets, Menus, and Backgrounds for users with that role $role->add_cap('edit_theme_options'); // For more CAP types and info, go to www.ads-software.com - Search for: ROLES and CAPABILITIES ?>
That’s it. Now login as a user with the Editor role to test it! (The jQuery Solution needs to be installed too!)
Hope you find this useful to your needs!
PS ~ This would make a great add-on feature or enhancement to any of the Role & Capabilities Plugins out there…
- The topic ‘jQuery Solves: How to allow Editors to edit Menus (only!)’ is closed to new replies.