下面是代码的更新版本,它还使用Transients API.
/**
* If post status is pending, set transient containing error message and
* change the redirect location.
* Filters slashed post data just before it is inserted into the database.
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*
*/
add_filter( \'wp_insert_post_data\', \'wpse242399_wp_insert_post_data\', 99, 2 );
function wpse242399_wp_insert_post_data( $data, $postarr ) {
if ( $data[\'post_type\'] !== \'revision\' && $data[\'post_status\'] === \'pending\' ) {
set_transient( get_current_user_id() . \'_wpse242399_post_pending_notice\',
__( \'Your post has been submitted for review.\', \'your-text-domain\' )
);
add_filter( \'redirect_post_location\', \'wpse242399_redirect_post_location\', 99 );
}
return $data;
}
/**
* Remove the filter for the redirect location
* and perform the redirect.
*/
function wpse242399_redirect_post_location( $location ) {
remove_filter( \'redirect_post_location\', __FUNCTION__, 99 );
return add_query_arg( [
\'post_type\' => \'event\',
\'author\' => get_current_user_id(),
], admin_url( \'edit.php\' )
);
}
/**
* Check to see if it\'s necessary to display a message.
* If so, delete the transient and output the message.
*/
add_action( \'admin_notices\', \'wpse242399_admin_notices\' );
function wpse242399_admin_notices() {
$message = get_transient( get_current_user_id() . \'_wpse242399_post_pending_notice\' );
if ( $message ) {
delete_transient( get_current_user_id() . \'_wpse242399_post_pending_notice\' );
printf( \'<div class="%1$s"><p>%2$s</p></div>\',
\'notice notice-success is-dismissible wpse242399_post_pending_notice\',
$message
);
}
}