16

I would like to run a function when the user clicks the "Empty Trash" button for posts. Something like:

add_action('empty_trash','myFunction');
function myFunction(){
// My code
}

2 Answers 2

17

I don't think there exist one, but you could create your own, wpse_empty_trash, with something like this:

/**
 * Add a custom hook 'wpse_empty_trash'
 */
add_action( 'load-edit.php', function()
{
    add_action( 'before_delete_post', function ( $post_id )
    {
        if (
            'trash' === get_post_status( $post_id ) 
            && filter_input( INPUT_GET, 'delete_all' )
            && 1 === did_action( 'before_delete_post ' )
        )   
            do_action( 'wpse_empty_trash' );
    } );
} );

Then you can use it with your code. Example:

add_action( 'wpse_empty_trash', 'myFunction' );
function myFunction() {
    // My code
}

Hopefully you can adjust this to your needs.

0

Yes.

https://developer.wordpress.org/reference/hooks/before_delete_post/

It’s important to note the hook runs only when the WordPress user empties the Trash. If you’re using this hook note that it will not fire if the user is deleting an Attachment, since attachments are force deleted, i.e., not sent to the Trash. Instead use the delete_post() hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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