WP-Admin: Allow Limited Admin Access to Different User Roles

👥

In this example, the wordpress user role of "contributor" or WooCommerce role of "shop_manager" is enabled to access limited Autoship Cloud submenu pages ('autoship_cloud_subpage_security' filter) or all submenu pages ('autoship_cloud_main_page_security' filter).

Both filters can be found in the autoship_create_menu() function.

Important: Always test customizations to your site on a staging environment before making changes to your live / production site. 

/**
* Adjusts the Security for the Autoship Sub Pages
* @link https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table
*
* @param string $capability The current required cap.
* @param string $menu_slug The Sub Menu Page.
* @return string $capability The filtered cap.
*/
function xx_adjust_autoship_cloud_subpage_security( $capability, $menu_slug ){

    // Allow users with a Contributor (shop manager) role to access Autoship Cloud > Scheduled Orders & Autoship Cloud > Reports pages
    return ( 'scheduled-orders' == $menu_slug ) || ( 'reports' == $menu_slug ) ? 'edit_posts' : $capability;
  
  }
  add_filter('autoship_cloud_subpage_security', 'xx_adjust_autoship_cloud_subpage_security', 10, 2 );
  
  /**
  * Adjusts the Security for the Autoship Main Menu Page
  * @link https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table
  *
  * @param string $capability The current required cap.
  * @return string $capability The filtered cap.
  */
  function xx_adjust_autoship_cloud_main_page_security( $capability ){
    
    // Allow users with a Contributor (shop manager)role to see ALL Autoship Cloud submenu pages
    return 'edit_posts';
  
  }
  add_filter('autoship_cloud_main_page_security','xx_adjust_autoship_cloud_main_page_security', 10, 1);