When editing a post using the admin interface the wp-admin/post.php script redirects you after saving the post. This is done to avoid resubmitting the post request if you refresh the page after submitting. It also means that you’re not able to output anything during the save_post
action.
case 'editpost':
check_admin_referer('update-post_' . $post_id);
$post_id = edit_post();
// Session cookie flag that the post was saved
if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
}
redirect_post($post_id); // Send user on their way while we keep working
exit();
You could do what WordPress does and use cookies to store the message you want to display
<?php
class Your_Class_Name_Here {
/**
* Setup
*/
static function setup() {
add_action( 'save_post', __CLASS__.'::save_post', 10, 2 );
add_action( 'in_admin_header', __CLASS__.'::display_message' );
}
/**
* Action: Save post
* hooked on save_post
* @param integer $post_ID
* @param WP_Post $post
*/
static function save_post( $post_ID, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( 'publish' !== $post->post_status ) {
return;
}
setcookie( 'Your_Class_Name_Here', 'display_message', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
}
/**
* Action: Display message
* hooked on in_admin_header
*/
static function display_message() {
if ( ! isset( $_COOKIE['Your_Class_Name_Here'] ) ) {
return;
}
if ( $_COOKIE['Your_Class_Name_Here'] != 'display_message' ) {
return;
}
// Delete the cookie
setcookie( 'Your_Class_Name_Here', null, -1, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
unset( $_COOKIE['Your_Class_Name_Here'] );
// Output the message
echo "Your message here";
}
}
Your_Class_Name_Here::setup();