Last active
May 13, 2024 13:26
-
-
Save robwent/916181ed9b979a6244a61b6d4b1a8865 to your computer and use it in GitHub Desktop.
Forcing W3 Total Cache to Clear Parent Category Listing Pages on Post Save
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Clear Total Cache term listing pages for a post | |
* @param $post_id | |
* @param $post | |
* | |
* @return void | |
*/ | |
function rw_clear_total_cache_terms_pages( $post_id, $post ) { | |
if ( function_exists( 'w3tc_flush_url' ) ) { | |
$taxonomies = get_object_taxonomies( $post->post_type ); | |
foreach ( $taxonomies as $taxonomy ) { | |
$terms = get_the_terms( $post_id, $taxonomy ); | |
if ( $terms ) { | |
foreach ( $terms as $term ) { | |
$urls = rw_get_post_term_urls( $term, $taxonomy ); | |
if ( ! empty( $urls ) ) { | |
foreach ( $urls as $url ) { | |
w3tc_flush_url( $url ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Recursively get the URLs of all terms up to the parent | |
* @param $term | |
* @param $taxonomy | |
* @param $urls | |
* | |
* @return array|mixed | |
*/ | |
function rw_get_post_term_urls( $term, $taxonomy, $urls = array() ) { | |
if ( $term ) { | |
$urls[] = get_term_link( $term, $taxonomy ); | |
if ( $term->parent ) { | |
$urls = array_merge( $urls, rw_get_post_term_urls( $term->parent, $taxonomy ), $urls ); | |
} else { | |
return $urls; | |
} | |
} | |
return $urls; | |
} | |
add_action( 'save_post', 'rw_clear_total_cache_terms_pages', 10, 2 ); | |
add_action( 'delete_post', 'rw_clear_total_cache_terms_pages', 10, 2 ); | |
add_action( 'publish_post', 'rw_clear_total_cache_terms_pages', 10, 2 ); | |
add_action( 'draft_post', 'rw_clear_total_cache_terms_pages', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment