Title: Implement warm on edit
Last modified: October 2, 2024

---

# Implement warm on edit

 *  [p15h](https://wordpress.org/support/users/prestonwordsworth/)
 * (@prestonwordsworth)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/implement-warm-on-edit/)
 * Currently, although the legend say `On their publication and edit`, the implementation
   doesn’t trigger the warmer upon edit.
 *     ```wp-block-code
       public function populate_enqueue( $new_status, $old_status, $post ) {        if ( 'publish' === $new_status ) {            $posts_enqueue = Cache_Warmer::$options->get( 'cache-warmer-posts-enqueue' );            if ( ! in_array( $post->ID, $posts_enqueue, true ) ) {                $posts_enqueue[] = $post->ID;                Cache_Warmer::$options->set( 'cache-warmer-posts-enqueue', $posts_enqueue );            }        }    }
       ```
   
 * Here’s an example of a trigger that does respond to an edit, from the CF page
   cache plugin:
 *     ```wp-block-code
       $purge_actions = array(      'deleted_post',                     // Delete a post      'wp_trash_post',                    // Before a post is sent to the Trash      'clean_post_cache',                 // After a post’s cache is cleaned      'edit_post',                        // Edit a post - includes leaving comments      'delete_attachment',                // Delete an attachment - includes re-uploading      'elementor/editor/after_save',      // Elementor edit      'elementor/core/files/clear_cache', // Elementor clear cache    );foreach ($purge_actions as $action) {      add_action($action, array($this, 'purge_cache_on_post_edit'), PHP_INT_MAX, 2);    }function purge_cache_on_post_edit($postId)  {    static $done = [];    if (isset($done[$postId])) {      return;    }    // Do not run this on the WordPress Nav Menu Pages    global $pagenow;    if ($pagenow === 'nav-menus.php') return;    if (($this->main_instance->get_single_config('cf_auto_purge', 0) > 0 || $this->main_instance->get_single_config('cf_auto_purge_all', 0) > 0) && $this->is_cache_enabled()) {      $current_action = function_exists('current_action') ? current_action() : "";      $this->modules = $this->main_instance->get_modules();      $error = '';      $validPostStatus = ['publish', 'trash', 'private'];      $thisPostStatus = get_post_status($postId);      if (get_permalink($postId) != true || !in_array($thisPostStatus, $validPostStatus)) {        return;      }      if (is_int(wp_is_post_autosave($postId)) || is_int(wp_is_post_revision($postId))) {        return;      }      if ($this->main_instance->get_single_config('cf_auto_purge_all', 0) > 0) {        $this->purge_all();        return;      }      $savedPost = get_post($postId);      if (is_a($savedPost, 'WP_Post') == false) {        return;      }      $urls = $this->get_post_related_links($postId);      $this->purge_urls($urls);      $this->modules['logs']->add_log('cache_controller::purge_cache_on_post_edit', "Purge Cloudflare cache for only post id {$postId} and related contents - Fired action: {$current_action}");      $done[$postId] = true;    }  }
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [p15h](https://wordpress.org/support/users/prestonwordsworth/)
 * (@prestonwordsworth)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/implement-warm-on-edit/#post-18050640)
 * I’ve now identified the reason why `populate_enqueue` doesn’t get triggered on
   post update: it’s because it’s using the `transition_post_status` hook, which
   only fires when you refresh the page, whereas post update is usually done via
   Ajax.
 * So, there’s two ways to go about this. One could use the `edit_post` hook like
   in the purge action above. Or one could register a custom hook:
 *     ```wp-block-code
       add_action('wp_ajax_' . WPACU_PLUGIN_ID . '_preload',     array($this, 'ajaxPreloadGuest'), PHP_INT_MAX);     /**                                                                                                             * This is triggered when /admin/admin-ajax.php is called (default WordPress AJAX handler)     */                                                                                                                            public function ajaxPreloadGuest()                                                                                 {        // Check nonce                                                            if ( ! isset( $_POST['wpacu_nonce'] ) || ! wp_verify_nonce( $_POST['wpacu_nonce'], 'wpacu_ajax_preload_url_nonce' ) ) {                      echo 'Error: The security nonce is not valid.';            exit();                                                                                                                            }                                                                                                                                                                                                                                                                         $pageUrl = isset($_POST['page_url']) ? $_POST['page_url'] : false;        $pageUrlDomain = parse_url($pageUrl, PHP_URL_HOST);                                                                                       $pageUrlPreload = add_query_arg( array( 'wpacu_preload' => 1 ), $pageUrl );                                                                    // Check if the URL is valid        if (! filter_var($pageUrlPreload, FILTER_VALIDATE_URL)) {            echo 'The URL '.$pageUrlPreload.' is not valid.';            exit();        }                                           // Check the domain from "page_url" parameter                           if (strpos(site_url(), $pageUrlDomain) === false) {            echo 'Error: Possible hacking attempt! The host name of the requested URL is not the same as the one of "Site Address (URL)" from "Settings" - "General".';            exit();                                 }                                                                                                   // Check privileges                                                                         if (! Menu::userCanAccessAssetCleanUp()) {            echo 'Error: Not enough privileges to perform this action.';            exit();        }                                                                                                        $response = wp_remote_get($pageUrlPreload);        if (is_wp_error($response)) {            // Any error generated during the fetch? Print it            echo 'Error: '.$response->get_error_code();        } else {            // No errors            echo 'Status Code: '.wp_remote_retrieve_response_code($response).' /  Page URL (preload): ' . $pageUrlPreload . "\n\n";            echo isset($response['body']) ? $response['body'] : 'No "body" key found from wp_remote_get(), the preload might not have triggered';        }        exit();    }    /**                                                                                                             * This is triggered when /admin/admin-ajax.php is called (default WordPress AJAX handler)     */                                                                                                                            public function ajaxPreloadGuest()                                                                                 {        // Check nonce                                                            if ( ! isset( $_POST['wpacu_nonce'] ) || ! wp_verify_nonce( $_POST['wpacu_nonce'], 'wpacu_ajax_preload_url_nonce' ) ) {                      echo 'Error: The security nonce is not valid.';            exit();                                                                                                                            }                                                                                                                                                                                                                                                                         $pageUrl = isset($_POST['page_url']) ? $_POST['page_url'] : false;        $pageUrlDomain = parse_url($pageUrl, PHP_URL_HOST);                                                                                       $pageUrlPreload = add_query_arg( array( 'wpacu_preload' => 1 ), $pageUrl );                                                                    // Check if the URL is valid        if (! filter_var($pageUrlPreload, FILTER_VALIDATE_URL)) {            echo 'The URL '.$pageUrlPreload.' is not valid.';            exit();        }                                           // Check the domain from "page_url" parameter                           if (strpos(site_url(), $pageUrlDomain) === false) {            echo 'Error: Possible hacking attempt! The host name of the requested URL is not the same as the one of "Site Address (URL)" from "Settings" - "General".';            exit();                                 }                                                                                                   // Check privileges                                                                         if (! Menu::userCanAccessAssetCleanUp()) {            echo 'Error: Not enough privileges to perform this action.';            exit();        }                                                                                                        $response = wp_remote_get($pageUrlPreload);        if (is_wp_error($response)) {            // Any error generated during the fetch? Print it            echo 'Error: '.$response->get_error_code();        } else {            // No errors            echo 'Status Code: '.wp_remote_retrieve_response_code($response).' /  Page URL (preload): ' . $pageUrlPreload . "\n\n";            echo isset($response['body']) ? $response['body'] : 'No "body" key found from wp_remote_get(), the preload might not have triggered';        }        exit();    }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Implement warm on edit’ is closed to new replies.

 * ![](https://ps.w.org/cache-warmer/assets/icon.svg?rev=2869611)
 * [Cache Warmer](https://wordpress.org/plugins/cache-warmer/)
 * [Support Threads](https://wordpress.org/support/plugin/cache-warmer/)
 * [Active Topics](https://wordpress.org/support/plugin/cache-warmer/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cache-warmer/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cache-warmer/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [p15h](https://wordpress.org/support/users/prestonwordsworth/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/implement-warm-on-edit/#post-18050640)
 * Status: not a support question