1

I want to clear the cache of a plugin on refreshing the preview of the WordPress customizer because I can only see the changes I make if the cache of that plugin is refreshed.

This because the theme mod values are stored in css files that need to have a refresh of the cache before you can see the changes. Since these css files are connected to theme mod values, I need to refresh the cache of the plugin on refreshing the customizer preview.

I have come this far with PHP:

function px_clear_bb_cache() {
FLBuilderModel::delete_asset_cache_for_all_posts();
}
do_action( 'customize_save', 'px_clear_bb_cache' );

The cache is now refreshed after saving and closing :( the customizer.

I think I need to hook into the customizer with JS + Ajax but can't figure out how to do this.

Would be great if someone could help me out here.

Basically the PHP command "FLBuilderModel::delete_asset_cache_for_all_posts();" needs to fire every time when refreshing the preview of the customizer.

  • Do you want to clear the plugin cache for all changes, even ones that aren't saved? – Welcher May 19 '17 at 10:51
  • Yes for all changes, even unsaved ones. – Bob May 19 '17 at 10:58
  • Why are you using do_action ? Shouldn't it be add_action( 'customize_save', 'px_clear_bb_cache' ); ? @Bob – CodeMascot May 19 '17 at 11:55
0

Got the solution:

  1. Create a PHP file "purge-bb-cache.php"
  2. Create a JS script like below in functions.php

purge-bb-cache.php

<?php 
  FLBuilderModel::delete_asset_cache_for_all_posts();
 ?>

JS script:

// Add JS to customizer
function print_customizer_js() {     
?>
<script>

// Purge BB Cache On Save Customizer
jQuery(document).ready(function(){
jQuery("input#save").click(function(){
//var ajaxurl = "<?php echo ( get_stylesheet_directory() . '/wp-content/themes/bb-theme-child/script.php' ); ?>";

jQuery.ajax({
type: 'POST',
url: '/wp-content/themes/bb-theme-child/assets/purge-bb-cache.php',
success: function(data) {
console.log("Purged BB Cache");
}
});
});
});
</script>
<?php
}
add_action('customize_controls_print_scripts', 'print_customizer_js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.