• Resolved Julian M

    (@julimuslia)


    Hello, i have multiple users with the role Editor in my website. I would like for them to Purge Cache and Minify Cache. At the moment this is valid only for administrator role. How can i activate it even for other roles such as Editor ?

    Thanks in advance,
    Julian

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support wpmansour

    (@wpmansour)

    By default, the Editor role does not have the ability to purge or minify cache in WP-Optimize. However, you can enable this functionality by adding a small piece of custom code to your site.

    Thread Starter Julian M

    (@julimuslia)

    Hi Mansour,

    thank you for your response. Could you please provide the code if possible?


    Thanks in advance,
    Julian

    Plugin Support wpmansour

    (@wpmansour)

    To handle your request of allowing certain roles (like Editors) to manage cache clearing while hiding the WP-Optimize sidebar for them, we’ve created two custom code snippets. The first snippet grants access to the Clear Cache feature for specified roles, and the second snippet hides the WP-Optimize sidebar menu for Editors to ensure they don’t have unnecessary access. You can directly add these codes to your theme’s functions.php file.

    1. Grant Access to “Clear Cache” for Specific Roles:

    This snippet lets you assign cache management permissions to specific roles (e.g., Editor). Simply modify the roles in the $roles array if needed.

    // Grant access to WP-Optimize Clear Cache feature for specific roles
    function wpo_grant_clear_cache_capability_to_roles() {
    // Define roles to grant access
    $roles = ['editor']; // You can add or remove roles by add comma role (, 'author') here

    foreach ($roles as $role_name) {
    $role = get_role($role_name);
    if ($role) {
    // Grant access to clear cache for the specified roles
    $role->add_cap('wpo_manage_cache');
    }
    }
    }
    add_action('init', 'wpo_grant_clear_cache_capability_to_roles');

    2. Hide the WP-Optimize Sidebar Menu for Editors:

    This snippet hides the WP-Optimize menu from Editors while still making it accessible to Administrators.

    // Hide WP-Optimize sidebar menu for Editors
    function wpo_hide_menu_for_editors() {
    // Check if the current user is an Editor and not an Administrator
    if (!current_user_can('administrator') && current_user_can('editor')) {
    remove_menu_page('WP-Optimize'); // The slug for WP-Optimize menu
    }
    }
    add_action('admin_menu', 'wpo_hide_menu_for_editors', 999);

    This hides the WP-Optimize sidebar menu from Editors, ensuring they don’t see the WP-Optimize menu in their dashboard.

    Note: To make sure these changes work as expected, ensure that “Enable the caching menu in the admin bar” is checked under the WP-Optimize settings in the admin panel. This will allow Editors (and other roles you specify) to access the cache-clearing function via the top admin bar menu

    If you prefer not to edit your theme’s functions.php file or are looking for a plugin-based solution, you can use a role management plugin like User Role Editor. This plugin allows you to easily manage capabilities for different roles, including enabling cache features and hiding menus.

    Feel free to let me know if you need further help or have any questions, Thanks you!

    Ben1098765

    (@ben1098765)

    Hi – I have the same requirement. I’ve added the above script to functions.php, but it didn’t work (nothing showing in the top admin bar). I then tried it with a completely separate and fresh installation of WordPress, and again nothing showed when logged in as Editor.

    I also tried the User Role Editor plugin in the the fresh installation as you also suggested, adding the “wpo_manage_cache” to the Editor role. That didn’t work either.

    In all cases “Enable the caching menu in the admin bar” was enabled in the settings, and “Enable page caching” was turned on.

    Any ideas what may be wrong? Are you able to make this work in a fresh installation?

    Thanks in advance!

    Plugin Support wpmansour

    (@wpmansour)

    Can you try adding this code to your theme’s functions.php file? It should give Editors the ability to manage WP-Optimize settings and hide the WP-Optimize sidebar for them:

    function wpo_grant_editor_manage_settings() {
    $role = get_role('editor'); // Get the Editor role
    if ($role) {
    $role->add_cap('wpo_manage_settings'); // Allows managing WP-Optimize settings
    $role->add_cap('wpo_run_optimizations'); // Allows running cache and optimizations
    }
    }
    add_action('init', 'wpo_grant_editor_manage_settings');

    // Hide WP-Optimize sidebar menu for Editors
    function wpo_hide_menu_for_editors() {
    // Check if the current user is an Editor but not an Administrator
    if (!current_user_can('administrator') && current_user_can('editor')) {
    remove_menu_page('wp-optimize'); // Slug for WP-Optimize menu
    }
    }
    add_action('admin_menu', 'wpo_hide_menu_for_editors', 999);

    Let me know if this works for you!

    Ben1098765

    (@ben1098765)

    Hi @wpmansour – thanks for the response! Unfortunately that didn’t work for me either. Logging out capabilities using Query Monitor and the admin_notices hook, I was able to determine that the capability was being set for the Editor role, but the top button just wasn’t showing.

    However, in case anyone may find this useful, I ended up fiddling around and came up with the following, which does work:

    // Function to add "Clear Cache" option to Editors
    function add_clear_cache_for_editors()
    {
    if (current_user_can('editor')) {
    // Add custom "Clear Cache" item in the Admin Bar
    add_action('admin_bar_menu', function ($wp_admin_bar) {
    $wp_admin_bar->add_node([
    'id' => 'clear-cache',
    'title' => 'Clear Cache',
    'href' => wp_nonce_url(admin_url('?action=editor_clear_cache'), 'editor_clear_cache_nonce')
    ]);
    }, 100);
    }
    }
    add_action('admin_init', 'add_clear_cache_for_editors');

    // Handle the cache clearing action
    function handle_editor_clear_cache()
    {
    if (isset($_GET['action']) && $_GET['action'] === 'editor_clear_cache' && current_user_can('editor')) {
    // Verify nonce for security
    check_admin_referer('editor_clear_cache_nonce');

    // Trigger WP Optimize cache-clearing function if available
    if (function_exists('wpo_cache_flush')) {
    wpo_cache_flush();
    wp_redirect(admin_url()); // Redirect to avoid repeat action on refresh
    exit;
    } else {
    wp_die('Cache clearing function not available.');
    }
    }
    }
    add_action('admin_init', 'handle_editor_clear_cache');

    I did use a smattering of chatgpt to help me to get this point, so no guarantees on the quality, but it works for me in this instance.

    Plugin Support wpmansour

    (@wpmansour)

    That’s awesome to hear you found a solution!

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.