Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Awesome!!! i am running into the same situation when saving custom metabox fields. any idea what i can use?? –  Prasath Nadarajah Jun 9 '12 at 11:45
add comment

1 Answer

up vote 19 down vote accepted

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 some of the answers already here on WA.

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

share|improve this answer
    
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 Jun 8 '11 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). –  TheDeadMedic Jun 8 '11 at 20:31
1  
Right on brother. Thanks for being there with your expertise! –  Tom Auger Jun 9 '11 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). –  The WebMacheter Dec 15 '11 at 6:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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