0

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
}
2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.