Support » Plugins » Hacks » publish_post hook multiple firing

  • 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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • So I guess the question is – is there anyway I can stop the hook from causing my code to be called more than once.

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

    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');

    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.

    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.

    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.

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

    But why should revisions cause that hook to fire?

    Because they hook into the same save_post function.

    Actually that doesn’t fix it – its a definite double fire of the “publish_post” hook when the post is actually published

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

    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.

    Good to know.

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

    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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘publish_post hook multiple firing’ is closed to new replies.