• Hi!
    I created hook ‘save_post’ and hang my function on it

    add_action('save_post', 'save_post');
    function save_post(){
      die('here we are');
    }

    Why if I press ‘add new’ this function is fired? It is normal behavior. In description of this function is written this will fire when post created or updated. In my case why it work without save and update. It will call twice.

    How to change this behavior? I need only one call when I save or update not when I create page…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter neo332

    (@neo332)

    I did this code:

    add_action('save_post', 'save_posta', 2, 10);
    function save_posta($post_id, $post, $update){
      echo $post_id;
      if ( wp_is_post_revision( $post_id ) )
        return;
    
      die('here we are');
    }

    and when press ‘add new’ got this:
    489here we are
    and when I update page it increment number, Why?
    Why does it work not like describe in cods link

    Moderator bcworkz

    (@bcworkz)

    It’s what it does. It’s a very generic action regardless of it’s name. You have more specific dynamic actions that fire from within wp_transition_post_status(), such as “draft_to_publish” etc.

    No matter what action you hook, even the specific dynamic ones, you should assume they could fire more than once for any given request. Many do, I don’t know why, I think it’s often due to what plugins do while hooked into the same actions.

    Your callback can simply remove itself from the action stack once it has done its thing. It’ll be reinstated for the next request. Use remove_action().

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘save_post hood fire when press button add new’ is closed to new replies.