WpW: The Purge All Hooks Setting

WordPress Wednesday: Purging LiteSpeed Cache via WordPress Hooks

Written by

in

WordPress Wednesday: Purging LiteSpeed Cache via WordPress Hooks

Welcome to another installment of WordPress Wednesday!
Today’s topic is: Purging LiteSpeed Cache via WordPress Hooks

Disclaimer: The information contained in this post is accurate for LSCWP v2.2.2 [release log]. If you are using a newer version of the plugin, some details may have changed. Please refer to our wiki for the latest!

—
Today we’re going to talk about a more advanced topic than usual. If you are unfamiliar with the concept of WordPress hooks and actions, then this may not be for you. If you want to learn more and then come back, that’s great, too. Here’s a really nice primer on the subject. You might also want to brush up on LiteSpeed Cache and how purging works.
—

Purging LiteSpeed Cache via WordPress Hooks

LiteSpeed Cache for WordPress is configured to automatically purge everything from the cache when certain WordPress hooks are run. For example: every time you change your theme (the switch_theme hook), or every time you create a new category or tag (the create_term hook), a purge_all action is triggered.

By default, LiteSpeed runs a purge_all action any time any of the following WordPress hooks occur:

switch_theme
wp_create_nav_menu
wp_update_nav_menu
wp_delete_nav_menu
create_term
edit_terms
delete_term
add_link
edit_link
delete_link

For most installations, this list of hooks is sufficient. Owners of highly-customized sites, however, may have reason to make changes to the list. We recently added a new setting that will allow you to do just that.

Purge All Hooks

Purging LiteSpeed Cache via WordPress Hooks: Polka Dot Cottage Crochet Hooks in a Row

From the WordPress Dashboard, navigate to LiteSpeed Cache > Settings > Advanced. Scroll down to Purge All Hooks, and you will see the list of hooks that currently trigger purge_all actions for your site, entered one per line.

You are free to edit this list. Remove hooks if you don’t want the cache purged, and add hooks if you want to automate more purges.

Removing a Hook

Let’s say you don’t want to purge the cache every time you create a new category.

Remove the create_term hook from the list, and press the Save Changes button.

(That was easy.)

Adding a WordPress Hook

This one is slightly more complicated, but only because you need to know the name of the hook you want to add.

For example, if you want to purge the cache any time a new comment is posted, then you need to know that the WordPress hook for posting a comment is comment_post.

Once you are aware of the name of the correct hook, you can insert it into the list on its own line.

The WordPress Code Reference provides a list of available hooks. There’s a handy search function on the page, too, in case you don’t want to go through the list alphabetically.

Adding a Plugin Hook

Plugins can provide hooks, too, and there may be a situation where you want to hang a purge_all onto a plugin hook. Again, you’ll need to do a little research to find the name of the hook, but once you do, it’s a simple matter of adding it to the Purge All Hooks list.

Let’s look at an example from our own plugin. You may have noticed that there are some tabs in the LiteSpeed Settings pages that suggest you should run a Purge All after you’ve changed those settings. Well, if changing the configuration is something you do frequently, why not automate that Purge All?

For example, LiteSpeed Cache > Settings > Optimize warns “After changing Minify/Combine settings, please do a Purge All action.” All we need to do is find the hook that runs when you Save Changes on the Optimization page, and add that hook to the Purge All Hooks list.

There is no hook specific to the Optimize page, but there is one that relates to nearly all of the ON/OFF switches on any of the Settings tabs: update_option_litespeed-cache-conf.

Add that hook to the list, and a Purge All will occur any time you enable or disable a setting.

Purging LiteSpeed Cache via WordPress Hooks: Polka Dot Cottage Crochet Hooks in a Jar

Finding a plugin’s hooks may not be easy. You can hope that they’ve provided a complete list as part of their documentation (which is rare), you can ask in their support forum (if they have one), or you can pore through their source code and try to find them (if it’s open source).

Here are some handy LiteSpeed plugin hooks. Most of these occur when individual settings (those not included in update_option_litespeed-cache-conf) are changed:

update_option_litespeed-cache-vary-group
update_option_litespeed-cache-exclude-optimization-roles
update_option_litespeed-cache-exclude-cache-roles
update_option_litespeed-optm-css
update_option_litespeed-optm-js-defer-excludes
update_option_litespeed-media-lazy-img-excludes
update_option_litespeed-media-need-pull
update_option_litespeed-env-ref
update_option_litespeed-cache-drop_qs
update_option_litespeed-cache-cdn_mapping
update_option_litespeed-cache-dns_prefetch
update_option_litespeed-cache-cloudflare_status
update_option_litespeed-log_ignore_filters
update_option_litespeed-log_ignore_part_filters
update_option_litespeed-object_global_groups
update_option_litespeed-object_non_persistent_groups
update_option_litespeed-crawler-as-uids
update_option_litespeed-adv-purge_all_hooks
update_option_litespeed-cdn-ori_dir
update_option_litespeed-media-webp_attribute

Please be sure that you understand fully what any hook means before adding it to Purge All Hooks. After all, purge_all is a drastic step. It wipes out the entire cache. You don’t want to automate that task unless the hook truly warrants it!

Changing Your Mind

If you’ve added some hooks, and you don’t like the results, you can go back to the default configuration. Copy the list of hooks from the “Recommended Value” box into the Purge All Hooks setting and press the Save Changes. Everything should go back to normal.

—

Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!

Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:

Comments

16 responses to “WpW: The Purge All Hooks Setting”

  1. Iakovos Frountas Avatar
    Iakovos Frountas

    What about adding a filter to disallow loading Litespeed minify on logged in users? It would be very useful for developers in order to change minor CSS parts on production sites.

    1. Lisa Clarke Avatar
      Lisa Clarke

      You could turn ON Disable all Features in the LiteSpeed Cache > Settings > Debug tab while you do your testing. It’s a little more aggressive than what you requested, but it will do the trick.

  2. Pascal Avatar
    Pascal

    Hi
    We use InfiniteWp to manage and update our WP Plugins. Those update seems to be silently run. And we discover that after those slient updates the purge all is not run?
    Thanks

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Pascal.

      If you can find out the name of the InfiniteWP hook that runs the silent updates, you can add that hook to the Purge All Hooks setting as described in this post. Theoretically, that should solve the problem.

  3. Pascal Avatar
    Pascal

    Hi
    This great plugin: https://wordpress.org/plugins/post-expiration-date/ turn to draft a post that was published.
    But the post still visible everywhere after goes to draft.
    So how purge the cache when a post changed from Publish to Draft?
    Thanks

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Pascal. For now, you can use the {old_status}_to_{new_status} hook (in this case, it’s publish_to_draft) to trigger a Purge All. There’s some documentation on that in the WP Codex.

      In the future, we can add this to the plugin, so that an un-publish acts just like a publish (i.e., purge the post and all related content automatically, without needing to trigger a more drastic Purge All).

      1. Pascal Avatar
        Pascal

        Hello
        I tried this below but it do not work:

        function on_trash_post( $post ) {
        if (method_exists( ‘LiteSpeed_Cache_API’, ‘purge_all’ ) && LiteSpeed_Cache_API::purge_all() ) {
        LiteSpeed_Cache_API::purge_all() ;
        }
        }
        add_action( ‘publish_to_trash’, ‘on_trash_post’, 10, 1 );

        1. Lisa Clarke Avatar
          Lisa Clarke

          Is there any reason why you aren’t using the method explained in this post? If you put publish_to_trash in the LiteSpeed Cache > Settings > Advanced > Purge All Hooks box, that should do what you want.

          1. Pascal Avatar
            Pascal

            Hi Lisa
            I was not aware of that, but I tried 🙂 But it did not worked, the cache has not been purged ather the post has been deleted bu the plugin: ttps://wordpress.org/plugins/post-expiration-date/
            🙁

          2. Lisa Clarke Avatar
            Lisa Clarke

            Originally you said that the plugin changes the format to Draft, not to Trash. Can you try using publish_to_draft in that field and see if that works?

          3. Pascal Avatar
            Pascal

            I tried using publish_to_draft but it did not changed anything…

          4. Lisa Clarke Avatar
            Lisa Clarke

            Ok. Can you drop by the #wpcache channel on Slack or create a ticket? We can troubleshoot there more easily than in blog comments. Thanks!

  4. Beat Markwalder Avatar

    Hi there, is there a specific to add for purge all alfter plugin’s update ? Is it possible to have a general hook for this ?

    1. Lisa Clarke Avatar
      Lisa Clarke

      If you navigate to LiteSpeed Cache > Cache > Purge and set the Purge All On Upgrade setting to ON, that should do what you want. Don’t forget to save your changes, too.

Leave a Reply

Your email address will not be published. Required fields are marked *