** Updated July 25, 2023 **
On certain themes, you may encounter issues where you have to Regenerate CSS & Data in the Elementor tools. If you hate having to go to the Elementor tools to regenerate the files and data, then these handy functions will help.
Elementor had updated the hook to clear the Elementor cache. it now uses:
\Elementor\Plugin::$instance->posts_css_manager->clear_cache();
I have also added a Quicklink on the WordPress top bar that can clear the Elementor cache, and regenerate the CSS. This is great so you don’t have to navigate to the Elementor tools to do this. Simply click on the link to clear the cache.
So now my updated code clears the Elementor cache every time you save your changes in the Elementor editor and also does it from the top of the admin dashboard.
I also included code to add the CSS in a separate file. Create a file in the same directory as your child’s function.php file, and name it “binarybb-styles.css”
That way if I share more cool snippets, we can just add the CSS to that file.
So in the CSS file, you will add the following CSS:
/* ***** Change the background color and padding to make it look like a button */
#wp-admin-bar-binarybb_regenerate_elementor_css > a {
background-color: #0073aa;
color: #ffffff;
padding: 5px 10px;
border-radius: 2px;
}
/* ***** Change the hover color */
#wp-admin-bar-binarybb_regenerate_elementor_css:hover > a {
background-color: #008ec2;
}
Minified version of the code above
#wp-admin-bar-binarybb_regenerate_elementor_css>a{background-color:#0073aa;color:#fff;padding:5px 10px;border-radius:2px}#wp-admin-bar-binarybb_regenerate_elementor_css:hover>a{background-color:#008ec2}
Here is the updated code you will add to your child theme’s function.php file:
// ***** Enqueue the Binary Blackboard CSS file.
function binarybb_enqueue_custom_admin_styles() {
wp_enqueue_style( 'bb-admin-styles', get_stylesheet_directory_uri() . '/binarybb-styles.css', [], '1.0.0' );
}
add_action( 'admin_enqueue_scripts', 'binarybb_enqueue_custom_admin_styles' );
// ***** Clear Elementor Cache after saving/updating.
function binarybb_clear_elementor_cache() {
if ( did_action( 'elementor/loaded' ) ) {
\Elementor\Plugin::$instance->posts_css_manager->clear_cache();
}
}
add_action('elementor/editor/after_save', 'binarybb_clear_elementor_cache');
// ***** Handles the action to clear the cache with the admin top bar link.
function binarybb_regenerate_elementor() {
check_admin_referer( 'binarybb_regenerate_elementor_action' );
if ( current_user_can( 'manage_options' ) && did_action( 'elementor/loaded' ) ) {
\Elementor\Plugin::instance()->files_manager->clear_cache();
wp_redirect( add_query_arg( 'binarybb_notice', 'elementor_regenerated', wp_get_referer() ) );
exit;
}
wp_safe_redirect( wp_get_referer() );
exit;
}
add_action( 'admin_post_binarybb_regenerate_elementor', 'binarybb_regenerate_elementor' );
// ***** Add the quicklink to the admin bar
function binarybb_custom_admin_bar_link() {
global $wp_admin_bar;
if ( current_user_can( 'manage_options' ) ) {
$wp_admin_bar->add_menu( array(
'id' => 'binarybb_regenerate_elementor_css',
'title' => 'Clear Elementor Cache',
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=binarybb_regenerate_elementor' ), 'binarybb_regenerate_elementor_action' )
) );
}
}
add_action( 'admin_bar_menu', 'binarybb_custom_admin_bar_link', 100 );
// ***** Display a notice when the regeneration is complete.
function binarybb_show_admin_notice() {
if ( isset( $_GET['binarybb_notice'] ) && $_GET['binarybb_notice'] === 'elementor_regenerated' ) {
$share_url = urlencode(get_permalink());
$share_text = urlencode('Automatically cleared the Elementor cache, here is how to do it.');
echo '
Elementor cache cleared & CSS Regenerated! Share the BinaryBlackboard love!
Tweet(X) this!
Share on Facebook!
';
}
}
add_action( 'admin_notices', 'binarybb_show_admin_notice' );
Please contact me if you encounter any issues
Bonus tip
You can also add cache
functions from your other caching plugins