• Resolved fransenbeheer

    (@fransenbeheer)


    Hi there, for WP Optimize I would like to add a filter to my functions that triggers cache purge for a specific page on a site update (new post or product for example).

    Something like ?

    add_filter( 'wpo_purge_all_cache_on_update', '__return_true' );

    but then with a page ID instead of all cache.

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support wpmansour

    (@wpmansour)

    Hi,

    Thank you for reaching out with your query about adding a filter to trigger a cache purge for a specific page when there’s a site update on WP Optimize.

    Based on your requirements, you can indeed use the WPO_Page_Cache::delete_single_post_cache($post_id); function to purge the cache for a specific page. This method allows you to target the cache of a single page or post efficiently.

    Here’s how you can implement this:

    function custom_purge_specific_page_cache($post_id) {
     // if ($post_id == 'your_specific_page_id') {
            WPO_Page_Cache::delete_single_post_cache($post_id);
     // }
    }
    add_action('save_post', 'custom_purge_specific_page_cache');

    This function hooks into WordPress’s save_post action, which is triggered whenever a post or page is saved. If you want this to only apply to a specific page, replace 'your_specific_page_id' with the ID of the page you’re targeting and uncomment the if statement.

    Add this code to your theme’s functions.php file or a site-specific plugin to ensure it executes correctly.

    If you have any further questions or need additional assistance with this setup, please feel free to let me know. I’m here to help!

    Best regards,
    Mansour M

    • This reply was modified 7 months, 1 week ago by wpmansour. Reason: clear display of code
    Thread Starter fransenbeheer

    (@fransenbeheer)

    Thanks for the quick reply Mansour!

    To be more clear, let me explain my goal. I display Woocommerce products by means of a shortcode on a page. I want to purge cache of that page anytime that I create a new product.

    And, would it also be possible to apply to a few specific page ID’s?

    Plugin Support wpmansour

    (@wpmansour)

    Thanks for the additional clarification! If you have a single page where you display WooCommerce products via a shortcode and you want to purge the cache of that specific page whenever a new product is created, the function can be simplified. Here’s an updated version of the code to meet this specific need:

    function custom_purge_on_new_product($post_id, $post, $update) {
    // Check if it's a new product
        if ($post->post_type == 'product' && !$update) {
    
    // Specify the ID of the page where the shortcode is displayed
        $page_id = 123; // Replace with your actual page ID
    
    // Purge the cache for the specified page
        WPO_Page_Cache::delete_single_post_cache($page_id);
      }
    }
    add_action('wp_insert_post', 'custom_purge_on_new_product', 10, 3);

    Replace 123 with the ID of the page that displays the WooCommerce products via your shortcode. This setup ensures that each time a new product is added, visitors to that page will see the most recent content without any caching issues.

    If in the future, you decide to display products on multiple pages and wish to purge several pages’ caches upon product creation, you can easily modify this snippet by turning $page_id into an array of page IDs and looping through it to purge each one.

    I hope this solution meets your needs effectively! If you have any more questions, need further modifications, or require assistance with anything else, please don’t hesitate to reach out. I’m here to help ensure everything works smoothly for you ??

    Best regards,
    Mansour M

    Thread Starter fransenbeheer

    (@fransenbeheer)

    Thanks again Mansour,

    One last question, how would it look like if I want to purge a few pages indeed, let’s say ID’s are 123, 124, 125?

    Plugin Support wpmansour

    (@wpmansour)

    Absolutely, I’m happy to help with that!

    To extend the cache purge to several specific pages, we can easily modify the code to include multiple IDs. Here’s an example that covers the page IDs you mentioned—123, 124, and 125:

    function custom_purge_multiple_pages_on_new_product($post_id, $post, $update) {
    // Check if it's a new product
     if ($post->post_type == 'product' && !$update) {
            
    // Array of page IDs where the cache needs to be purged
     $page_ids = [123, 124, 125]; // Feel free to modify this list 
    
    // Loop through the list and purge cache for each page
       foreach ($page_ids as $page_id) {
                WPO_Page_Cache::delete_single_post_cache($page_id);
            }
        }
    }
    add_action('wp_insert_post', 'custom_purge_multiple_pages_on_new_product', 10, 3);

    Hope this helps! If you have any more questions or need further assistance, please don’t hesitate to reach out.

    Best regards,
    Mansour M

    Thread Starter fransenbeheer

    (@fransenbeheer)

    This works great for new entries. Is it also possible to do the purge when an existing product is being updated or set to concept etcetera?

    (because I noticed that my test product still was displayed after being unpublished)

    Plugin Support wpmansour

    (@wpmansour)

    Hello,

    I’m glad to hear that the solution works well for new products! To extend the functionality so that the cache is also purged when an existing product is updated or changed in status (like being unpublished), you can modify the function to react to updates as well.

    Here’s how you can adjust the code to handle changes in existing products:

    function custom_purge_pages_on_products($post_id, $post, $update) {
        
    // Check if it's a product
       if ($post->post_type == 'product') {
            
    // Array of page IDs where the cache needs to be purged
       $page_ids = [123, 124, 125]; // Add or remove page IDs as needed
    
    // Check if the product is being updated or unpublished
       if ($update || $post->post_status == 'draft' || $post->post_status == 'pending') {
                
    // Loop through each page ID and purge its cache
       foreach ($page_ids as $page_id) {
                    WPO_Page_Cache::delete_single_post_cache($page_id);
                }
            }
        }
    }
    add_action('save_post', 'custom_purge_pages_on_products', 10, 3);
    

    This updated function checks not only for new products but also for updates to existing products, including changes to statuses like draft or pending, which typically include actions like unpublishing a product. Whenever any of these changes occur, it will purge the cache for the specified pages.

    Please add or modify the page IDs in the $page_ids array to match those where your products are displayed. This should ensure that your product listings are always current, reflecting the latest updates and status changes.

    Best Regards,
    Mansour M

    Thread Starter fransenbeheer

    (@fransenbeheer)

    Thank you so much for this fantastic support!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘WP Optimize create filter for specific page purge’ is closed to new replies.