WordPress.org

Ready to get started?Download WordPress

Forums

jQuery Solves: How to allow Editors to edit Menus (only!) (3 posts)

  1. BULLSH.com
    Member
    Posted 1 year ago #

    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 wordpress.org - 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...

  2. BULLSH.com
    Member
    Posted 1 year ago #

    Oops! I forgot about the Admin Bar...

    You will want to use the code below in the Theme's footer.php file, so you can also control which sub-menus are available when a user (Editor) is logged in and on the website.

    <?php
      //  Using jQuery: How to allow Editors to edit only Menus (or more!)
      //  Placed in THEME's footer.php as the Admin Bar is added when a user is logged in
    
      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:
          jQuery('li#wp-admin-bar-themes').remove();
    
          // CUSTOMIZE:
          jQuery('li#wp-admin-bar-customize').remove();
    
          // WIDGETS:
          jQuery('li#wp-admin-bar-widgets').remove();
    
          // MENUS:
          // jQuery('li#wp-admin-bar-menus').remove();
    
          // BACKGROUND:
          jQuery('li#wp-admin-bar-background').remove();
        });
          </script>
    <?php
        } // End IF current_user_can...
      } // End IF is_user_logged_in...
    ?>
  3. Santiago Ti
    Member
    Posted 8 months ago #

    Thank you, I have not tried it yet but it looks promising.
    keep up the good work
    and thank you for sharing

Topic Closed

This topic has been closed to new replies.

About this Topic