0

I am building a custom template which uses a cache of blade template files that it creates. While working, I was making changes in the customizer and was confused why my changes weren't showing. It took me a while to figure out it was because the blade files were cached so the layout changes weren't taking affect.

So, for in the future, I would like to be able to have a "clear cache" button in the Wordpress Customizer that I could click to delete the cache directory. But I can't figure out how to do so.

This theme is built off of the "Sage 9" Wordpress template in case that matters.

Here is my customizer control (may or not be right) in /app/admin.php

$wp_customize->add_control(
    new \WP_Customize_Control(
        $wp_customize,
        'clearcache',
        [
            'label'      => __( 'Clear Cache', 'starcresc' ),
            'section'    => 'layout',
            'settings'   => 'clearCache',
            'type'       => 'button',
            'choices'    => [
                '0' => __( 'No (Default)' ),
                'Clear Cache' => __( 'Yes' ),
            ]
        ])
);

Here is my function that I add to the functions.php file which should delete the cache files stored in siteurl/wp-content/uploads/cache

function sc_clearcache() {
    return
    $upload_dir = wp_upload_dir();
        $files = glob($upload_dir . 'cache/*'); // get all file names
        foreach($files as $file){ // iterate files
            if(is_file($file))
            unlink($file); // delete file
    }
};

But I have no idea how to connect the two and make the button actually call the function.

EDIT:

/* Clear the Cache */
function sc_clearcache() {
    $upload_dir = wp_upload_dir();
    $files = glob($upload_dir . 'cache/*'); // get all file names
    foreach($files as $file){ // iterate files
        if(is_file($file)) {
            unlink($file); // delete file
        }
    }
}
do_action( 'customize_save_after', 'sc_clearcache' );
0

The cache should really get cleared based on some change you made, not based on some manual cache clearing. Can you not disable caching in the Customizer preview by turning it off when is_customize_preview()? Then you can flush the cache at the customize_save_after action:

add_action( 'customize_save_after', 'sc_clearcache' );
1
  • That would work fine, but after changing my function (see update) it still does not fire. Any ideas what I'm doing wrong?
    – chris98.5
    Dec 21 '17 at 17:16

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.