Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to hook a function only when I delete permanently a post from the database, I've tried the 'before_delete_post' hook, however, it's called both when it's trashed and permanently deleted.

The wp_delete_post() function calls wp_trash_post() and should stop processing, but it looks like the wp_delete_post() is called again after the post is trashed.

I've seen this question: Wordpress Delete hook with wp_delete_post function?. My need is exactly the oposite of it.

share|improve this question

2 Answers 2

before_delete_post is not called when a post is only trashed.

While wp_delete_post() can trash posts (if the post is not trashed and its not being forcibly deleted): it does so by calling wp_trash_post() and exiting the function prior to the triggering the action before_delete_post.

I've tested this, the following function will only 'die' when you permanently delete a post, but not when its confined to the trash. The wp_die is to demonstrate when the call is being made - I do not recommend using it on a live site.

add_action('before_delete_post', 'my_deleted_post');
function my_deleted_post($post_id){
   wp_die(var_dump($post_id));
};

The wp_delete_post() function can be found here (WP 3.3.1)

share|improve this answer
    
It should work, however, when it is used from the admin page, click in 'trash' within the post's list, the 'before_delete_post' is being called. I don't know why. –  romulodl Jan 23 '12 at 12:53
    
Do you mean the actions that appear in the table? I tried that, and trashing with the drop-down bulk actions. It worked for me. What version of WP are you using? Is it on a clean install as well? Some themes/plug-ins may be misbehaving. –  Stephen Harris Jan 23 '12 at 12:55
    
I'm did exactly the same action as your example and it is being called when the post is trashed. I'm using WP 3.2 and now checking whether another plugin could be screwing everything up. –  romulodl Jan 23 '12 at 13:39
    
BINGO! It is a WP multisite and I'm using the "Sitewide Tags" Plugin, it's deleting permanently the post from the main site, this is why 'before_delete_post' is being called! Can I edit your answer and add 'Double check whether you have a clean instalation and another plugin is not hooked to "trash_post"'? Sthephen, thank you very much for your help! –  romulodl Jan 23 '12 at 13:55
    
Absolutely, glad you got it sorted. –  Stephen Harris Jan 23 '12 at 14:01
up vote 0 down vote accepted

The issue was the "Sitewide tags" plugin which was misbehaving the natural WP flow. A function called sitewide_tags_post_delete() is hooked on 'trash_post' and it's calling wp_delete_post() to delete the post on the main blog.

Following the @Stephen's approach, I did this:

add_action('before_delete_post', 'my_deleted_post');
function my_deleted_post($post_id){
  global $blog_id;   
  if($blog_id == <main_blog_id>){ // usually is 1
    return false;
  }

  //do whatever you need :)
};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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