• Resolved MarkJ123

    (@markj123)


    Hi, I am trying to alter some content when a post is created.
    In my plugin I am hooking into the WordPress event by calling

    add_action('save_post','insert_method')

    Where my insert_method is below

    function insert_method( $post_id ) {

    The issue I am having is that this appears to fire multiple times when a post is created and therefore the code inside the method fires as well.

    I have tried to do the following

    if ( !wp_is_post_revision( $post_id ) )

    but unfortunately it still does not work.

    This is my first plugin attempt so I am still learning but any help / advice about how to fix the problem would be appreciated 🙂

    Regards
    Mark

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m afraid multiple action firings is a fact of life in WP. There are some post status transition actions that might fire less often, but it’s not safe to assume they would fire only once. Your code needs to account for the multiple firings somehow no matter what.

    How this is done would vary by what is being done, so it’s hard to be specific. One general possibility though is to use a transient to act as a flag on whether to do something or not. Set an expiration time long enough to span multiple firings but short enough that a legitimate post update is not missed due to an old transient.

    Thread Starter MarkJ123

    (@markj123)

    Thanks for that 🙂 I’ll adjust my code to take this into account
    Best wishes
    Mark

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Newbie: add_action('save_post') Question’ is closed to new replies.