Writing an admin notice after saving a post in WordPress

July 11, 2017

I recently had to write an alert to a user after the saving of an ACF options page. I did so by using the WordPress admin_notices action.


add_action( 'save_post', 'test_function', 100, 1 );
function test_function( $post_id ) {
  // Save an option for the admin notice
  add_option( 'my_notice_text', 'My Message' );
};
add_action('admin_notices', 'my_admin_notices');
function my_admin_notices() {
  // If a notice exists then echo it as a notice.
  if ( get_option('my_notice_text') ) {
    echo "

" . get_option( 'my_notice_text' ) . "

"; delete_option('my_notice_text'); } }

This is a Knowledge Base post.