2

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

  • What is your function trying to do? How are you sure it's not firing? – EAMann May 24 '13 at 16:36
  • Because it wasn't echoing. But the answer below describes why that was the case. So it must have been a bug in something else I had written. I should hopefully be able to figure it out now though. – bob_hardy May 24 '13 at 16:57
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');
|improve this answer|||||
  • that does end up printing stuff to the screen. it kills everything else, but it does allow me to double check what's happening. thx – bob_hardy May 24 '13 at 16:55
  • Right. Being able to see the debugging output is the the entire point. You can't leave it that way. You could send that output to the log file but I typically don't. It rarely seems worth the effort. – s_ha_dum May 24 '13 at 16:56
  • 1
    You can also send the debug info per HTTP-Header and use your browsers debugger to watch this. – fuxia May 24 '13 at 18:53

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.