updating a post does not clear homepage cache
-
Hello. Since version 1.4 when you update a post the homepage cache is not cleared anymore. Editors can not launch a complete clear cache and it would not even right. Any advice?
-
You’re right, this was intentionally changed in version 1.4.0 to ensure cache clearing on post updates is handled on the post level. This was done because the logic controlling the cache clearing on any post type publishing was also clearing the cache on any post type updates.
At the time I had thought it may be a good idea to extend the post level caching beyond only clearing the page or complete cache on post updates, which turns out it may be. I will need to think about what would be best. Further updates to follow. In the meantime you could add the following snippet to your
functions.php
file to achieve this (replacing0
with the ID of your home page):function clear_home_page_cache_on_save_post() { $home_page_id = 0; do_action( 'ce_clear_post_cache', $home_page_id ); } add_action( 'save_post', 'clear_home_page_cache_on_save_post' );
Hi,
I’m also facing the same issue.
I tried adding the function you mentioned to fix the problem, but since I don’t use a static homepage, my homepage doesn’t have an ID. So I cannot use the ce_clear_post_cache action without an ID.
I have to admit that not being able to automatically clear the homepage cache on post update makes the plugin pretty unusable.
Would you have another solution / suggestion?
Thanks.
To temporarily fix the issue, I added my own action in cache_enabler.class.php:
add_action( 'ce_clear_post_cache_url', array( __CLASS__, 'clear_page_cache_by_url' ) );
I then replaced ce_clear_post_cache with ce_clear_post_cache_url in the code you provided, and it now clears the home URL when I update a post.
I know my code will get overwritten on the next update, so hopefully you will have figured out a solution by then.
Thank you.
-
This reply was modified 4 years, 6 months ago by
janvitos.
You’re right @janvitos. The following snippet can be used instead to also cover the home page even if it’s not static:
function clear_home_page_cache_on_save_post() { if ( class_exists( 'Cache_Enabler' ) ) { Cache_Enabler::clear_home_page_cache(); } } add_action( 'save_post', 'clear_home_page_cache_on_save_post' );
This behavior will be improved. You can follow the progress on this in issue #112 on GitHub.
Thanks for that @coreyk.
But I’m still not sure if that function will work (I will have to do some testing).
I noticed clear_home_page_cache() uses the get_site_url() function. My WordPress installation is NOT located in the root directory of my server, so get_site_url() returns something like this: https://www.website.com/wordpress/. So in my case (and in the case of others I’m sure), I need to use the get_home_url() function instead, which returns the root directory without the WordPress installation directory (https://www.website.com).
Maybe you could consider using get_home_url() instead in the clear_home_page_cache() function, or make some kind of check if get_site_url() or get_home_url() needs to be used. I believe the get_home_url() function always returns the root of the website, which would normally represent the cached homepage to be purged.
EDIT: Just as I thought, the clear_home_page_cache() function doesn’t work because of its usage of get_site_url().
Here’s the final code I used in my functions.php without having to modify cache_enabler.class.php:
function clear_home_page_cache_on_save_post() { if ( class_exists( 'Cache_Enabler' ) ) { Cache_Enabler::clear_page_cache_by_url( get_home_url() ); } } add_action( 'save_post', 'clear_home_page_cache_on_save_post' );
This code will survive updates, so it’s a pretty solid solution for now.
Thanks for your help!
-
This reply was modified 4 years, 6 months ago by
janvitos.
-
This reply was modified 4 years, 6 months ago by
janvitos.
-
This reply was modified 4 years, 6 months ago by
janvitos.
-
This reply was modified 4 years, 6 months ago by
janvitos.
-
This reply was modified 4 years, 6 months ago by
janvitos.
-
This reply was modified 4 years, 6 months ago by
janvitos.
Thanks for the great suggestion, @janvitos. Using
get_home_url()
instead ofget_site_url()
would be a much better choice to resolve the issues that arise in the event WordPress is installed in a subdirectory and the Site Address (URL) setting is changed. I’ve opened and closed issue #113 that addresses this. This change will be released in version 1.4.8.I’m happy I could provide some assistance.
Thanks.
could i use this way to solve my problem that when publishing new posts the pagination is also deleted instead of just the home page?
function clear_home_page_cache_on_save_post() { if ( class_exists( 'Cache_Enabler' ) ) { Cache_Enabler::clear_page_cache_by_url( 'https://www.site.com/page/*' ); } } add_action( 'publish_post', 'clear_home_page_cache_on_save_post' );
does a * work as placeholder in this case?
@mobiflip until we improve this in #112 you could use the following to achieve that:
function clear_pagination_cache_on_publish_post() { if ( class_exists( 'Cache_Enabler' ) ) { Cache_Enabler::clear_page_cache_by_url( 'https://www.example.com/page/', 'dir' ); } } add_action( 'publish_post', 'clear_pagination_cache_on_publish_post' );
Instead of
*
you’ll want to pass'dir'
so the entire directory (meaning all subpages) are also cleared instead of just that single page.thanks. that helps me a lot.
is there a way to define multiple urls in this function?
-
This reply was modified 4 years, 6 months ago by
René.
Version 1.5 has been released, with it the following has changed:
- Removed publishing action in sidebar on Classic Editor in favor of clearing the page cache of any updated post type by default (e.g.
post
,page
,product
, etc.). This means page cache will always be cleared in any way that any post type is updated, whether that is an editor, such as the Classic Editor or Gutenberg, or even an API. - Add new method to clear the associated cache. This will clear the cached pages that might contain a page that has been cleared. Currently this is the post type archives (e.g. home page or defined posts page, shop page, etc.), taxonomies archives (e.g. categories and tags), author archives, and date archives.
- Update the default behavior to clear the associated cache when any post type is published (instead of only the home page cache).
- Update the default behavior to clear the page and associated cache when any published post type is updated (instead of the page or complete cache).
- Update the default behavior to clear the page and associated cache when any post type is trashed (instead of the complete cache).
- Add new
pagination
clear type to clear the pagination page(s) cache (e.g.https://www.example.com/blog/page/2/
orhttps://www.example.com/blog/seite/2/
). This will ensure the pagination is cleared on any archives page.
When I update a post (custom post type), the homepage cache is not cleared. Shouldn’t that happen now?
Is your home page your defined posts page (default WordPress configuration) or the post type archives page for your custom post type?
The standard page, which also displays the cpt chronologically
-
This reply was modified 4 years, 6 months ago by
- The topic ‘updating a post does not clear homepage cache’ is closed to new replies.