Run php file after post_updated in WordPress

Issue

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.

Answered By – Orbital

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published