Wordpress - How to add an admin notice upon post save/update
The reason this doesn't work is because there is a redirection happening after the save_post action. One way you can acheive want you want is by implementing a quick work around using query vars.
Here is a sample class to demonstrate:
class My_Awesome_Plugin {
public function __construct(){
add_action( 'save_post', array( $this, 'save_post' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
public function save_post( $post_id, $post, $update ) {
// Do you stuff here
// ...
// Add your query var if the coordinates are not retreive correctly.
add_filter( 'redirect_post_location', array( $this, 'add_notice_query_var' ), 99 );
}
public function add_notice_query_var( $location ) {
remove_filter( 'redirect_post_location', array( $this, 'add_notice_query_var' ), 99 );
return add_query_arg( array( 'YOUR_QUERY_VAR' => 'ID' ), $location );
}
public function admin_notices() {
if ( ! isset( $_GET['YOUR_QUERY_VAR'] ) ) {
return;
}
?>
<div class="updated">
<p><?php esc_html_e( 'YOUR MESSAGE', 'text-domain' ); ?></p>
</div>
<?php
}
}
Hope this helps you a little bit. Cheers
Made a wrapper class for this kind of scenario. Actually the class can be used in any scenario involving displaying notices. I use the PSR standards, so the naming is atypical of Wordpress code.
class AdminNotice
{
const NOTICE_FIELD = 'my_admin_notice_message';
public function displayAdminNotice()
{
$option = get_option(self::NOTICE_FIELD);
$message = isset($option['message']) ? $option['message'] : false;
$noticeLevel = ! empty($option['notice-level']) ? $option['notice-level'] : 'notice-error';
if ($message) {
echo "<div class='notice {$noticeLevel} is-dismissible'><p>{$message}</p></div>";
delete_option(self::NOTICE_FIELD);
}
}
public static function displayError($message)
{
self::updateOption($message, 'notice-error');
}
public static function displayWarning($message)
{
self::updateOption($message, 'notice-warning');
}
public static function displayInfo($message)
{
self::updateOption($message, 'notice-info');
}
public static function displaySuccess($message)
{
self::updateOption($message, 'notice-success');
}
protected static function updateOption($message, $noticeLevel) {
update_option(self::NOTICE_FIELD, [
'message' => $message,
'notice-level' => $noticeLevel
]);
}
}
Usage:
add_action('admin_notices', [new AdminNotice(), 'displayAdminNotice']);
AdminNotice::displayError(__('An error occurred, check logs.'));
The notice is displayed once.
In addition to @jonathanbardo's answer which is great and functions well, if you want to remove the query argument after the new page is loaded, you can use the removable_query_args filter. You get an array of argument names to which you can append your own argument. Then WP will take care of removing all of the arguments in the list from the URL.
public function __construct() {
...
add_filter('removable_query_args', array($this, 'add_removable_arg'));
}
public function add_removable_arg($args) {
array_push($args, 'my-query-arg');
return $args;
}
Something like:
'...post.php?post=1&my-query-arg=10'
Will become:
'...post.php?post=1'