{"id":12687,"date":"2023-06-19T06:00:06","date_gmt":"2023-06-19T10:00:06","guid":{"rendered":"https:\/\/blog.litespeedtech.com\/?p=12687"},"modified":"2023-06-19T06:00:06","modified_gmt":"2023-06-19T10:00:06","slug":"lscwp-api-cache-hooks","status":"publish","type":"post","link":"https:\/\/lswp.store\/index.php\/2023\/06\/19\/lscwp-api-cache-hooks\/","title":{"rendered":"Managing Cacheability With LSCWP API"},"content":{"rendered":"<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-12689\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable.png\" alt=\"LSCWP API Cache Tags\" width=\"1000\" height=\"500\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable.png 1000w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable-300x150.png 300w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable-768x384.png 768w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable-600x300.png 600w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>The LiteSpeed Cache plugin for WordPress includes an API that you can use to customize cache behavior. If you\u2019re a developer, the LSCWP API will help you to achieve LiteSpeed compatibility within your plugins. If you\u2019re a site owner who enjoys customizing every little aspect of your site, maybe you have some unique reason to use one of our API hooks in your child theme\u2019s <code>functions.php<\/code> file.<\/p>\n<p>Whatever your actual purpose is, today we\u2019re going to look at three LSCWP API cache hooks and show you how to use them to control the cacheability of a page:<\/p>\n<ul>\n<li><code>litespeed_control_set_nocache<\/code><\/li>\n<li><code>litespeed_control_set_cacheable<\/code><\/li>\n<li><code>litespeed_control_force_cacheable<\/code><\/li>\n<\/ul>\n<p>Plus, we&#8217;ll look at a couple of bonus hooks that are useful for setting cache scope:<\/p>\n<ul>\n<li><code>litespeed_control_set_private<\/code><\/li>\n<li><code>litespeed_control_force_public<\/code><\/li>\n<\/ul>\n<h2>To cache, or not to cache<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12695\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/intro.jpg\" alt=\"\" width=\"500\" height=\"486\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/intro.jpg 500w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/intro-300x292.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>First, let\u2019s look at how LSCWP decides which pages to cache and which pages not to cache.<\/p>\n<p>By default, the <code>wp<\/code> action hook is run on each page of a WordPress site, after the query has been parsed and the post has been loaded, but before template functionality is executed. (There may be exceptions to this rule, but we\u2019ll talk about that later.)<\/p>\n<p>LSCWP considers all pages that have had the <code>wp<\/code> action run on them to be cacheable. Essentially, every page is cacheable. Unless it falls into one of these categories:<\/p>\n<ul>\n<li>It is an admin page<\/li>\n<li>It is a <code>POST<\/code> request<\/li>\n<li><code>is_trackback()<\/code> is true<\/li>\n<li><code>is_search()<\/code> is true<\/li>\n<li>No theme is used<\/li>\n<\/ul>\n<p>Site administrators may also set some content to be non-cacheable, so LSCWP checks the values of the settings in <strong>LiteSpeed Cache &gt; Cache &gt; Excludes<\/strong>, and considers a page non-cacheable if:<\/p>\n<ul>\n<li>The URI is found in the <strong>Do Not Cache URIs<\/strong> list<\/li>\n<li>The URL has a query string found in the <strong>Do Not Cache Query Strings<\/strong> list<\/li>\n<li>The post has a category found in the <strong>Do Not Cache Categories<\/strong> list<\/li>\n<li>The post has a tag found in the <strong>Do Not Cache Tags<\/strong> list<\/li>\n<li>The request has a cookie found in the <strong>Do Not Cache Cookies<\/strong> list<\/li>\n<li>The request has a user agent found in the <strong>Do Not Cache User Agents<\/strong> list<\/li>\n<li>The request is being made by a user whose role is checked in the <strong>Do Not Cache Roles<\/strong> list<\/li>\n<\/ul>\n<p>This is enough for most admins, but maybe you have a special case that you want to handle programmatically. Or maybe you have written a plugin that generates its own pages and doesn\u2019t follow the usual <code>wp<\/code> action hook conventions. What do you do then?<\/p>\n<p>You use LSCWP\u2019s cache control API hooks.<\/p>\n<h2>Cache control API hooks<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-large wp-image-12693\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-1024x866.jpg\" alt=\"\" width=\"1024\" height=\"866\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-1024x866.jpg 1024w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-300x254.jpg 300w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-768x650.jpg 768w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-1536x1299.jpg 1536w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-1320x1117.jpg 1320w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks-600x508.jpg 600w, https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/hooks.jpg 1564w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>Mark the current page as cacheable<\/h3>\n<p>If your plugin generates pages and does not invoke the <code>wp<\/code> action hook, then LSCWP may not know that the page should be cached. Use the <code>litespeed_control_set_cacheable<\/code> action with a &#8216;reason&#8217; parameter, like so:<\/p>\n<pre>do_action(\n\t'litespeed_control_set_cacheable',\n\t'The lady doth protest too much, methinks.'\n);\n<\/pre>\n<p>Note that if the page fits one of the criteria specified in the the plugin\u2019s <strong>Cache &gt; Excludes<\/strong> settings, then the page may ultimately be set as non-cacheable, despite this hook.<\/p>\n<h3>Mark the current page as non-cacheable<\/h3>\n<p>The <code>litespeed_control_set_nocache<\/code> action is the opposite of the previous one. Use it in your plugins or snippets to inform LSCWP not to cache the page.<\/p>\n<pre>do_action(\n\t'litespeed_control_set_nocache',\n\t'And flights of angels sing thee to thy rest!'\n);\n<\/pre>\n<h3>Force the current page to be cacheable<\/h3>\n<p>The <code>litespeed_control_force_cacheable<\/code> action informs LSCWP to cache the page, and it <em>will<\/em> override anything the user may enter in the <strong>Cache &gt; Excludes<\/strong> settings.<\/p>\n<pre>do_action(\n\t'litespeed_control_force_cacheable',\n\t'if it be not to come, it will be now'\n);\n<\/pre>\n<h2>What have I done?<\/h2>\n<p>With all the settings and API hooks available, it can be difficult to know whether the current page is set to be cached or whether it is non-cacheable. Use the <code>litespeed_control_cacheable<\/code> filter to get a definitive answer.<\/p>\n<pre>apply_filters( 'litespeed_control_cacheable', false );\n<\/pre>\n<h2>BONUS<\/h2>\n<p>Once you set a page as cacheable, you might want to further define the scope of that cache. Do you want to store the cache object in a logged-in user\u2019s private cache, or do you want all visitors to view a publicly cached copy? By default, LSCWP caches pages publicly. Use these action hooks to explicitly define cache scope:<\/p>\n<pre>do_action(\n\t'litespeed_control_set_private',\n\t'Listen to many, speak to a few.'\n);\n\ndo_action(\n\t'litespeed_control_force_public',\n\t'Brevity is the soul of wit.'\n);\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>We hope this is helpful to you plugin developers and site tinkerers out there. If you want to learn more about LSCWP\u2019s API hooks, take a look at our <a href=\"https:\/\/docs.litespeedtech.com\/lscache\/lscwp\/api\/#cache-control\">API Reference<\/a>.<\/p>\n<p>\u2014<br \/>\nThank you to Tynan Beatty for his numerous contributions to this post, including all of the sample code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A look at three LSCWP API cache hooks which control the cacheability of a page for your plugin or other functions.<\/p>\n","protected":false},"author":1,"featured_media":12689,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[38,316],"class_list":["post-12687","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cache","tag-api","tag-wordpress"],"jetpack_featured_media_url":"https:\/\/lswp.store\/wp-content\/uploads\/2023\/06\/lscwp-cacheable.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/12687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/comments?post=12687"}],"version-history":[{"count":0,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/12687\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media\/12689"}],"wp:attachment":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media?parent=12687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/categories?post=12687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/tags?post=12687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}