Show admin notice if fields empty after saving post WordPress

  Kiến thức lập trình

For my custom metabox fields I want create an error message by saving the post if they are empty .
Wordpress has a hook for creating notices admin_notices

I try to use this hook into the saving hook from WordPress but this does not showing up.

add_action('save_post', function($post_id){
    if($_POST['pdesc'] == ''){
        add_action( 'admin_notices', function(){
            echo '<div class="notice notice-warning is-dismissible">Test error message</div>';

I found this on the internet but this does not working

 * Add a flash notice to {prefix}options table until a full page refresh is done
 * @param string $notice our notice message
 * @param string $type This can be "info", "warning", "error" or "success", "warning" as default
 * @param boolean $dismissible set this to TRUE to add is-dismissible functionality to your notice
 * @return void
function add_flash_notice( $notice = "", $type = "warning", $dismissible = true ) {
    // Here we return the notices saved on our option, if there are not notices, then an empty array is returned
    $notices = get_option( "my_flash_notices", array() );
    $dismissible_text = ( $dismissible ) ? "is-dismissible" : "";
    // We add our new notice.
    array_push( $notices, array( 
            "notice" => $notice, 
            "type" => $type, 
            "dismissible" => $dismissible_text
        ) );
    // Then we update the option with our notices array
    update_option("my_flash_notices", $notices );
 * Function executed when the 'admin_notices' action is called, here we check if there are notices on
 * our database and display them, after that, we remove the option to prevent notices being displayed forever.
 * @return void
function display_flash_notices() {
    $notices = get_option( "my_flash_notices", array() );
    // Iterate through our notices to be displayed and print them.
    foreach ( $notices as $notice ) {
        printf('<div class="notice notice-%1$s %2$s"><p>%3$s</p></div>',
    // Now we reset our options to prevent notices being displayed forever.
    if( ! empty( $notices ) ) {
        delete_option( "my_flash_notices", array() );
// We add our display_flash_notices function to the admin_notices
add_action( 'admin_notices', 'display_flash_notices', 12 );

I try to use this hook into the saving hook from WordPress but this does not showing up.

add_action(‘save_post’, function($post_id){
if($_POST[‘pdesc’] == ”){
add_flash_notice( __(“My notice message, this is an error, but, it is not dismissible”), “error”, false );

Who can help me with the puzzle?