John Davidson

Run php file after post_updated in WordPress

0 comments
Message:


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 );


My Answer:
To run a PHP file after a post is updated in WordPress, you can use the `post_updated` action hook. Here's how you can modify your code to achieve this:

php
function run_php_file_after_post_updated( $post_ID, $post_after, $post_before ) {
// Check if the updated post is a WooCommerce product
if ( 'product' === $post_after->post_type ) {
// Run the PHP script to create the XML file
include_once( 'path/to/your/php/script.php' );
}
}

add_action( 'post_updated', 'run_php_file_after_post_updated', 10, 3 );


In the above code:
- The `run_php_file_after_post_updated` function is hooked to the `post_updated` action with a priority of 10 and 3 parameters.
- Inside the function, we check if the updated post is a WooCommerce product by comparing the post type.
- If the updated post is a product, we include the PHP script that creates the XML file.

Make sure to replace `'path/to/your/php/script.php'` with the actual path to your PHP script that creates the XML file.

By using this code, your PHP script will be executed every time a WooCommerce product is updated in WordPress.

Rate this post

4 of 5 based on 6350 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us