Puddinq.com sharing knowledge

How can you allow editors to clear (manage) the WP Rocket cache

How can you allow editors to clear (manage) the WP Rocket cache

First of, you should look at the official documentation customize-access WP Rocket. This article uses the same technique but adds a trick. When you use the init hook, the function runs every time the hook fires. To reduce this, we have added an option and before we run the full function we check if that option has been set.

One could question if we should check if the role has the capability already and if the option has been set. For a commercial plugin this would be better to avoid people running into troubles. but getting the option and checking for capabilities might be slower than just running the function.

 * Allow editor WP Rocket capabilities
function puddinq_rocket_add_purge_posts_to_author()
    // check if acces has been granted
    if (get_option('your_site_editor_rocket_cap') != 'granted') {

        // gets the author role object
        $role = get_role('editor');

        // add a new capability
        // WP Rocket

        // set option access has been granted
        update_option('your_site_editor_rocket_cap', 'granted');

add_action('init', 'puddinq_rocket_add_purge_posts_to_author', 12);