WordPress.org

Ready to get started?Download WordPress

Forums

publish_post hook multiple firing (17 posts)

  1. Steve
    Member
    Posted 2 years ago #

    my plugin has a hook into publish_post:

    add_action('publish_post', 'my_function',20);

    On some WP installs this seems to fire multiple times when a post is published. Has anyone else seen this problem? I'm wondering if its down to some other plugin upsetting things as I can't get my blog to do it.

  2. Steve
    Member
    Posted 2 years ago #

    So I guess the question is - is there anyway I can stop the hook from causing my code to be called more than once.

  3. Steve
    Member
    Posted 2 years ago #

    Surely someone has some idea if there is a fix / workround for this problem....

  4. The reason it is firing multiple times is because the hook is designed to fire whenever the post is published or updated, and possibly also when it is saved.

    What you need to do is use three hooks together:

    add_action('pending_to_publish', 'your_function');
    add_action('draft_to_publish', 'your_function');
    add_action('new_to_publish', 'your_function');
  5. Steve
    Member
    Posted 2 years ago #

    I want it to fire when ever the post is changed so I have to use the publish_post hook That isn't the problem - what I'm finding is that for some reason on some installs the

    add_action('publish_post', 'my_function',20);

    call causes my function to be called multiple times during a single actual publish / update (usually within a few seconds of each other) and putting individual hooks in place (including publish_to_publish) doesn't solve the problem. And its that multiple firing that I need to stop. The Codex for the publish_post hook states:

    'Runs when a post is published, or if it is edited and its status is "published". Action function arguments: post ID. '

    Which is exactly what I need - I just need to make sure it only fires once.

  6. Ah, I see. I'm not sure, but I think this might actually be a bug in the latest WP versions. I have been experiencing some interesting behavior with the _publish hooks recently, but have not yet diagnosed what is going on.

  7. Steve
    Member
    Posted 2 years ago #

    I think it is a bug - something seems to make the publish hooks fire more than once. I'm trying to diagnose it myself too - not sure if it happens unless you've got a specific other plugin enabled as I can't seem to reproduce it.

  8. yalamber
    Member
    Posted 2 years ago #

    It is due to revisions. try doing
    if(!wp_is_post_revision($post_ID)) {
    //your code here
    }

  9. Steve
    Member
    Posted 2 years ago #

    But why should revisions cause that hook to fire?

  10. Because they hook into the same save_post function.

  11. Steve
    Member
    Posted 2 years ago #

    Actually that doesn't fix it - its a definite double fire of the "publish_post" hook when the post is actually published

  12. Hmm, interesting. It might be a bug in core.

  13. Steve
    Member
    Posted 2 years ago #

    An update - if the post is scheduled then it doesn't happen. It only happens when you publish directly from the Web UI Edit/Add post screen.

  14. Good to know.

  15. Steve
    Member
    Posted 2 years ago #

    But as to what makes it happen I have no idea .. its not something simple as a rapid double click on the publish button

  16. Steve
    Member
    Posted 2 years ago #

    I worked out a fix. My code checks for a global variable to see if its there, if it is then it knows its running already and stops, if not it sets it and then runs the code, and removes it at the end. Not the most elegant of methods but it works.

  17. That should work well. I've definitely done similar things for other issues like this.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.