最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Can I differentiate between "Delete Post Permanently" and "Empty Trash"

programmeradmin0浏览0评论

I'm currently working on developing a plugin with a custom post type of "Dealer Location". (Not relevant to question, just a bit of background)

When a post is in the trash and "delete permanently" is pressed, I need to remove this piece of meta from each user.

When a post/posts are in the trash and "empty trash" is pressed, I need to run through EACH location being deleted and do something.

I have an extremely basic function just to test when it runs right now, as I have all the other nuts & bolts (removing user meta, etc) figured out..:

public function wcmdl_remove_deleted_location_users(){

    die;
} 

And the action I'm using is..:

$this->loader->add_action('before_delete_post', $plugin_admin, 'wcmdl_remove_deleted_location_meta');

I've tried using the hook 'delete_post' too and in both cases the function fires when expected. However, it fires on BOTH "delete permanently" AND "empty trash".

Is there any sort of hook or if statement I can use to differentiate between mass-deleting and single-deleting in this instance? If not, would it be viable to pass some value via ajax/js depending on which button is pressed, and use it to determine the action taken in a function via an if/else statement?

I'm currently working on developing a plugin with a custom post type of "Dealer Location". (Not relevant to question, just a bit of background)

When a post is in the trash and "delete permanently" is pressed, I need to remove this piece of meta from each user.

When a post/posts are in the trash and "empty trash" is pressed, I need to run through EACH location being deleted and do something.

I have an extremely basic function just to test when it runs right now, as I have all the other nuts & bolts (removing user meta, etc) figured out..:

public function wcmdl_remove_deleted_location_users(){

    die;
} 

And the action I'm using is..:

$this->loader->add_action('before_delete_post', $plugin_admin, 'wcmdl_remove_deleted_location_meta');

I've tried using the hook 'delete_post' too and in both cases the function fires when expected. However, it fires on BOTH "delete permanently" AND "empty trash".

Is there any sort of hook or if statement I can use to differentiate between mass-deleting and single-deleting in this instance? If not, would it be viable to pass some value via ajax/js depending on which button is pressed, and use it to determine the action taken in a function via an if/else statement?

Share Improve this question edited May 4, 2019 at 1:11 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked May 3, 2019 at 21:17 Syd McArdleSyd McArdle 437 bronze badges 4
  • Isn't it you just hook into the delete_post action and then get the current post ID and perform your custom stuff? Then it doesn't matter if it's a mass deletion, no? As this will be triggered for every single post, no? – norman.lol Commented May 3, 2019 at 21:22
  • @leymannx this actually appears to be working, looks like I just overthought it! Thanks a bunch - if you wanna leave a proper answer, I will mark it off as correct as soon as I can. – Syd McArdle Commented May 3, 2019 at 23:27
  • Well, I was just asking directionally I guess. I'm actually curious how exactly you got hold of the post ID in the end to trigger your custom stuff. Maybe you can add and accept an answer yourself? – norman.lol Commented May 3, 2019 at 23:31
  • 1 @leymannx No worries, so I wrote up an answer for this detailing the steps I took without /all/ the specifics of the function that runs upon deletion, because it's a big beefy one and involves different processes based on post hierarchy. To get the post ID, I wound up using a simple variable-assigned "get_the_ID();". I returned these values on single and multiple deletes to make sure the IDs that were being returned were the ones expected, and it reflected the single ID/list of IDs accurately each time. – Syd McArdle Commented May 16, 2019 at 17:07
Add a comment  | 

1 Answer 1

Reset to default 2

So as leymannx pointed out in his comment, the "delete_post" hook actually handles both scenarios I was looking for, as it seems to know to run a "for each post deleted, do x" when "Empty Trash" is clicked and will also "do x" when "Delete Post Permanently" is clicked!

My final code ended up looking something like..:

    public function wcmdl_remove_deleted_location_users(){

      $currentpost    = get_the_ID();
      $parentid       = wp_get_post_parent_id( $currentpost );

      if( get_post_type() == 'dealer_location' ) {
        //do stuff involving $currentpost and $parentid here...
      }
    }

Keeping in mind that this is being used within a standard plugin boilerplate format and NOT directly in the functions.php file of a child theme, this hook was placed in the "includes>class-[plugin name].php" file..:

        $this->loader->add_action('delete_post', $plugin_admin, 'wcmdl_remove_deleted_location_users');

If you were to be trying to do the same via your functions.php, the structure for this would instead be:

add_action('delete_post', 'wcmdl_remove_deleted_location_users');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论