admin管理员组

文章数量:1287869

I understand that save_post hook can fire various times, based on autosaving, revisions, etc.

With that in mind, I have a save_post hook set up as

add_action( 'save_post', 'do_stuff', 5000, 2 );
function do_stuff($post_id, $post) {

At the start of the function I have

    if (isset($post->post_status) && 'auto-draft' == $post->post_status) {
        return;
    }

    // Autosave, do nothing
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // AJAX? Not used here
    if (defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    if( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) return;

I thought this would ensure one call to the hook. But upon hitting Update on a post edit, I see from some logging that I am doing that the hook is called twice.

Maybe it has something to do with the fact that I have a custom Gutenberg block plugin I am using to extend a core block? In my plugin php file I don't have anything other than loading of the plugin's js. So I am not sure how the plugin would play a role here.

In any case, just curious if I am missing something obvious...

Thanks

I understand that save_post hook can fire various times, based on autosaving, revisions, etc.

With that in mind, I have a save_post hook set up as

add_action( 'save_post', 'do_stuff', 5000, 2 );
function do_stuff($post_id, $post) {

At the start of the function I have

    if (isset($post->post_status) && 'auto-draft' == $post->post_status) {
        return;
    }

    // Autosave, do nothing
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // AJAX? Not used here
    if (defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    if( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) return;

I thought this would ensure one call to the hook. But upon hitting Update on a post edit, I see from some logging that I am doing that the hook is called twice.

Maybe it has something to do with the fact that I have a custom Gutenberg block plugin I am using to extend a core block? In my plugin php file I don't have anything other than loading of the plugin's js. So I am not sure how the plugin would play a role here.

In any case, just curious if I am missing something obvious...

Thanks

Share Improve this question asked Sep 15, 2021 at 15:54 BrianBrian 3372 silver badges11 bronze badges 6
  • How are you logging/testing this theory? Why does it matter that it runs twice? What is the problem that this solves? – Tom J Nowell Commented Sep 15, 2021 at 16:41
  • I just use the write_log function to see when it is being called. In save hooks, I sometimes do various database manipulations, or interact with some service like Mailchimp, and really don't want to be re-running actions that I only want to do once. – Brian Commented Sep 15, 2021 at 18:09
  • how do you know that it's the same request though? That hook may not be the ideal place to do those things as it could significantly slow down saving. Also, if a user clicks update, then immediately updates again, you'll get the same problem but triggered by a human. If you only want something to happen once, you need to make sure it sets a flag or a note somewhere saying that it's happened so that the next time it sees that flag and doesn't do it again. Are you sure these are things you want to happen on post save and not post publish? – Tom J Nowell Commented Sep 15, 2021 at 21:24
  • The types of things I am saving are things like a webinar start date. I might not have that field filled out at publish time. Later if I update and set that field, I trigger an api call to Zoom to set up corresponding data in Zoom. The hook is save_post_myposttype, I see it called twice, but haven't done backtrace to see how it is being called twice. I thought about setting flag somewhere so that I exit, but was trying to avoid that, and instead rely on some hook that I can rely on running once. – Brian Commented Sep 16, 2021 at 5:59
  • you can switch from save_post to a specific status transition, but I don't believe you can use hooks to differentiate between quick successive save requests and ones that are hours or days apart, they're all classed as post updates. If they're on separate requests then trying to use variables wouldn't help. You could try and optimise it by comparing the old meta to the new meta and only making requests to zoom if the values are different but that'd be a different question and I don't remember the specifics off the top of my head – Tom J Nowell Commented Sep 16, 2021 at 9:30
 |  Show 1 more comment

1 Answer 1

Reset to default 2

save_post becomes unwieldy under Gutenberg, in my experience. It fires at least twice, and none of the checks you have in place will distinguish between instances.

I would consider replacing it with wp_after_insert_post, which was introduced into core for this very reason.

本文标签: Why is savepost hook being called twice despite all my efforts