当使用管理界面编辑帖子时,wp admin/post。php脚本在保存帖子后重定向您。这样做是为了避免在提交后刷新页面时重新提交post请求。这也意味着在save_post
行动
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();
您可以像WordPress那样,使用Cookie来存储要显示的消息
<?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();