38

I'm surprised by the fact that my function that I've tacked onto the save_post action fires when I click the "New Post" link in the Admin Dashboard. Note - this is before I've pressed Save or Update, and it fires immediately, not after an elapsed time or auto-update.

On the other hand, when I then type in something and press the Publish or Update or Save Draft buttons, the echo statement I've put inside my action handler does not echo out, so it appears that the action is NOT firing at any other time. This may be unrelated.

Here's my code:

add_action('save_post', 'MyNS\save_event_metabox', 10, 2);
function save_event_metabox($post_id, $post){
  echo "<h1>YES!</h1>";
}

This YES echoes (at the top of the page) when I press the "New Post" link but does NOT echo when I type something and then press Update or Publish or Save Draft. This seems to contradict the documentation on the save_post action and the wp_insert_post() function.

Can anyone clear this up for me?

1
  • Awesome!!! i am running into the same situation when saving custom metabox fields. any idea what i can use?? Commented Jun 9, 2012 at 11:45

1 Answer 1

46

When you click 'New Post', you're simply loading the page wp-admin/post-new.php.

In doing so, WordPress will always create a new post (an 'Auto Draft') to ensure all other features (such as media uploads) and plugins work as normal, even before you actually save a draft or publish the post.

And this, in turn, triggers save_post. Hence your echo.

Okay, so why don't I get an echo when updating or publishing?

In between saving and the following page load, WordPress is actually sending a GET redirect back to the same page, which appears transparent (you can witness this with an HTTP monitor, such as HttpFox).

In other words;

  1. You click Update or Publish
  2. Browser sends data to server
  3. WordPress handles it, and in the process triggers save_post
  4. WordPress sends back a redirection header, and exits, before any browser output occurs (including your echo)*
  5. Browser follows redirection and loads 'edit post' page.

The redirect might seem unnecessary (since you could just POST to the same page), but it's part of a technique known as Post/Redirect/Get in avoiding duplicate form submissions.

If you're attempting to print out custom messages based on a result of a function hooked to save_post, check out these questions/answers.

*Not strictly true, your echo will in fact occur before the redirect header is sent, but the browser will either discard it, or things happen so quickly it never renders.

5
  • Outstanding. Thanks for such a complete and detailed answer! In the interest of learning more, how did you come across this knowledge in the first place?
    – Tom Auger
    Commented Jun 8, 2011 at 20:14
  • Through the same frustration you were experiencing ;) Get hold of a good IDE (I use phpDesigner) and just delve into the relevant admin file(s). Commented Jun 8, 2011 at 20:31
  • 1
    Right on brother. Thanks for being there with your expertise!
    – Tom Auger
    Commented Jun 9, 2011 at 15:20
  • Awesome piece of information. I was going nuts because in my local server it seemed to work "ok", but not in my production server... looks like in my local server it wasn't applying the post/redirect/get technique properly because it wasn't sending the redirect header (don't know why though). Commented Dec 15, 2011 at 6:20
  • Thanks for the answer, I was hoping to leverage the $update parameter to determine if the save event was the first save event or an update event, but have learned that all manual save events will have $update as true. Is that the case? I'm wondering if there is a graceful workaround without comparing post status states which might be dynamic. (This so far is my favorite answer: wordpress.stackexchange.com/a/365727/2798)
    – atwellpub
    Commented Feb 5, 2021 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.