Question

How can I make wordpress call this function when a post is saved?

$folder = "/temp/files/";   //path of the folder to be removed  

function delFolder($folder) 
{
foreach(glob($folder . '/*') as $file)  //takes the content of the given folder
{ 
    if(is_dir($file))       //check if it is another folder then recursively call itself on it
        delFolder($file); 
    else 
        unlink($file);      //check if it is file then deletes it
} 
rmdir($folder);             //removes the folder
}
Was it helpful?

Solution

Add this to your theme's functions.php file:

function wpse202681_save_post_action($post_id, $post_object, $update) {
    $folder = "/temp/files/";
    delFolder($folder);
}
add_action( 'save_post', 'wpse202681_save_post_action', 10, 3 );
// + delFolder() function definition

You will probably have to alter folder paths depending on your /temp/files/ location.

This will execute everytime any post is saved. You can restrict it to only some post type, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top