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;
- You click
Update
orPublish
- Browser sends data to server
- WordPress handles it, and in the process triggers
save_post
- WordPress sends back a redirection header, and exits, before any browser output occurs (including your echo)*
- 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.