Search code examples
phpwordpresswoocommercecron

Run php file after post_updated in WordPress


I wrote a PHP script to create and XML of all my WooCommerce products. This runs every night via a cronjob and works fine. But I want it to run after post_updated, how can I manage to do this? Can't figure it out :(

function check_values(){
    //Here it needs to run the specific PHP file to create the new XML after for example updating the product price
}
add_action( 'post_updated', 'check_values', 10, 3 );

Solution

  • Set the cron script to run every 10 minutes instead of once a day

    At the beginning of your xml generation script put a check

    $status = get_option( 'cron_job_xml' );
    
    if($status != 'on') {
    exit;   
    } else {
    update_option( 'cron_job_xml', 'off', 'no' );
    }
    

    And this code to functions.php

    function check_values(){
    
    update_option( 'cron_job_xml', 'on', 'no' );
    
    }
    add_action( 'post_updated', 'check_values', 10, 3 );
    

    Now when we update the post, we will change the value of option And the cron script will check the value and run only when there are updates.