Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W3 Total Cache - Allow Editors To Purge Single Post From Cache

I have been looking for a way to allow my editor to purge a post from the page cache.

The problem:

I am making meta description updates that do not get updated upon clicking "post update" and sometimes the titles do not get updated without purging the post from the page cache too. However, it goes through if I click update twice.

The problem is the editor role do not have permissions to purge a post from the cache.

What I have tried:

I found that some users were able to create a script, but I can't find the code for it in the FAQs. https://wordpress.org/support/topic/plugin-w3-total-cache-can-non-admin-users-clear-the-cache

Secondly, I read a couple mentions to fragment caching, but I don't see any further documentation on it.

Third, I tried to follow changing user roles, but it didn't see safe for the plugin: https://wordpress.org/support/topic/allow-editors-to-flush-cache-from-toolbar

Fourth, I found some other interest in this, but it did not get solved: https://wordpress.org/support/topic/editor-user-level-able-to-purge-cache

Fifth, on StackOverflow, they have restricted it from certain users. Which means it's possible, but not a direct solution? disable "Purge from Page Cache" for specific roles on w3-total-cache

Asking for:

I would like to find a way for the editor role to empty the page cache for a single post. If you could provide a work around like a separate page script to do it manually or way to tweak the permissions with a functions.php addition, I would appreciate it very much.

like image 205
DomainsFeatured Avatar asked Mar 12 '23 14:03

DomainsFeatured


1 Answers

I needed a similar thing, but I'm sure you can adapt this to your needs:

First some filters to make it show the "Performance"-Menu on the AdminBar for normal editors:

function allow_users_to_flush($capability) {
   return "publish_post";
}
add_filter("w3tc_capability_row_action_w3tc_flush_post", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_w3tc_flush", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_w3tc_flush_all", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_admin_bar", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_admin_bar_flush_all", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_admin_bar_w3tc", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_admin_bar_flush", "allow_users_to_flush", 10, 10);
add_filter("w3tc_capability_w3tc", "allow_users_to_flush", 10, 10);

And, because the user must be allowed to access the dashboard for purging/flushing, a cap-filter:

function w3tc_cap_filter( $allcaps, $cap, $args ) {
    if(preg_match("/w3tc_dashboard/", $_SERVER["REQUEST_URI"])) {
        $allcaps[$cap[0]] = true;
    }
    return $allcaps;
}
add_filter( 'user_has_cap', 'w3tc_cap_filter', 10, 3 );

Voila, the user now sees the menu and can flush the cache (I needed "Purge all caches", not a single post/page, but that should work the same).

My permissions are pretty broad, but it works for my case. You might want to add more safeguards depending on your requirements. For reference:

http://hookr.io/plugins/w3-total-cache/0.9.5/filters/#index=w has a nice overview of filters in w3.

https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap for info on the capability-filter.

like image 199
janh Avatar answered Apr 08 '23 14:04

janh