wp_trash_post not firing

I want to call a function when a post is placed into the trash. I want to do something more complicated than the following, but have simplified to what’s below just to see if it’ll work and it doesn’t.

function gna_my_function() {
    echo 'trashed ';
}
add_action('wp_trash_post', 'gna_my_function');

Thanks

Related posts

1 comment

  1. There are two hooks you should be considering– wp_trash_post and trashed_post. Based on your statement that you want this to work “when a post is placed into the trash” I’d suggest the latter is the better hook, since it runs after the post is successfully placed in the trash.

    I hate to suggest this as an answer but I do think it explains why your function does not appear to output anything. When WordPress saves a post a request is sent to the server, the request is processed, and then the browser is redirected back to the originating page. That means that you cannot always see dumped/echoed output without killing the script. I believe the following should make your apparently debugging function do what you expect it to, provided that you have “trash” enabled.

    function gna_my_function() {
        echo 'trashed '; die;
    }
    add_action('wp_trash_post', 'gna_my_function');
    

Comments are closed.