Created
October 9, 2025 05:03
-
-
Save mlbd/1af905fc184d496532858b2f95c9dd48 to your computer and use it in GitHub Desktop.
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
| /** | |
| * A) Regenerate Elementor CSS for a specific post. | |
| * | |
| * Compatible with Elementor 3.31.x and 3.32.x | |
| * Clears element cache in addition to regenerating CSS | |
| */ | |
| function ml_el_regenerate_post_css( $post_id ) { | |
| if ( empty( $post_id ) || wp_is_post_revision( $post_id ) ) return; | |
| if ( ! class_exists( '\Elementor\Plugin' ) ) return; | |
| $run = function() use ( $post_id ) { | |
| $plugin = \Elementor\Plugin::instance(); | |
| // Only regenerate if the document is built with Elementor | |
| if ( method_exists( $plugin->documents, 'is_built_with_elementor' ) | |
| && $plugin->documents->is_built_with_elementor( $post_id ) ) { | |
| // Clear element cache for this post (new in 3.32) | |
| if ( class_exists( '\Elementor\Core\Base\Document' ) ) { | |
| $document = $plugin->documents->get( $post_id ); | |
| if ( $document && method_exists( $document, 'delete_cache' ) ) { | |
| $document->delete_cache(); | |
| } | |
| } | |
| // Also clear from the cache manager if available | |
| if ( isset( $plugin->posts_css_manager ) | |
| && method_exists( $plugin->posts_css_manager, 'clear_cache' ) ) { | |
| $plugin->posts_css_manager->clear_cache(); | |
| } | |
| // Regenerate CSS | |
| if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { | |
| $css = new \Elementor\Core\Files\CSS\Post( $post_id ); | |
| if ( method_exists( $css, 'update' ) ) { | |
| $css->update(); // Rebuilds post-{ID}.css | |
| } | |
| } | |
| // Clear post meta cache to ensure fresh data | |
| wp_cache_delete( $post_id, 'post_meta' ); | |
| // Trigger cache clear action for compatibility | |
| do_action( 'elementor/core/files/clear_cache' ); | |
| } | |
| }; | |
| did_action( 'elementor/loaded' ) ? $run() : add_action( 'elementor/loaded', $run, 99 ); | |
| } | |
| /** | |
| * B) Clear Elementor's global cached CSS/JS (like Tools → Regenerate Files & Data). | |
| */ | |
| function ml_el_clear_global_css_js_cache() { | |
| if ( ! class_exists( '\Elementor\Plugin' ) ) return; | |
| $run = function() { | |
| $plugin = \Elementor\Plugin::instance(); | |
| // Clear files manager cache | |
| if ( isset( $plugin->files_manager ) | |
| && method_exists( $plugin->files_manager, 'clear_cache' ) ) { | |
| $plugin->files_manager->clear_cache(); | |
| } | |
| // Clear posts CSS manager cache | |
| if ( isset( $plugin->posts_css_manager ) | |
| && method_exists( $plugin->posts_css_manager, 'clear_cache' ) ) { | |
| $plugin->posts_css_manager->clear_cache(); | |
| } | |
| // Clear element cache (new in 3.32) | |
| // This is crucial for v3.32.4 where Element Caching is now default | |
| if ( class_exists( '\Elementor\Core\Cache\Cache' ) ) { | |
| $cache = \Elementor\Core\Cache\Cache::instance(); | |
| if ( method_exists( $cache, 'flush' ) ) { | |
| $cache->flush(); | |
| } | |
| } | |
| // Alternative method for element cache | |
| if ( isset( $plugin->db ) | |
| && method_exists( $plugin->db, 'save_plain_editor' ) ) { | |
| // Force database update by triggering cache invalidation | |
| do_action( 'elementor/core/files/clear_cache' ); | |
| } | |
| // Clear WordPress object cache for Elementor | |
| wp_cache_flush(); | |
| }; | |
| did_action( 'elementor/loaded' ) ? $run() : add_action( 'elementor/loaded', $run, 99 ); | |
| } | |
| /** Approach A: per-post CSS regenerate on OWF complete */ | |
| add_action( 'owf_revision_workflow_complete', function( $post_id ) { | |
| $original_post_id = get_post_meta( $post_id, '_oasis_original', true ); | |
| if( ! empty( $original_post_id ) ) { | |
| ml_el_regenerate_post_css( $original_post_id ); | |
| // Additional safety: also clear global cache to ensure consistency | |
| ml_el_clear_global_css_js_cache(); | |
| } else { | |
| ml_el_clear_global_css_js_cache(); | |
| } | |
| }, 11, 1 ); | |
| /** Approach B: global cache clear on OWF complete (enable if you prefer global purge) */ | |
| // add_action( 'owf_revision_workflow_complete', function( $post_id ) { | |
| // ml_el_clear_global_css_js_cache(); | |
| // }, 11, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment