You can try the given approach to achieve the desired results. Here is the update code you need to use.
/**
* This is the action to check a condition before the post is updated.
*/
add_action( 'pre_post_update', 'check1', 10, 2 );
function check1( $post_id, $data ) {
if (1 == 1) {
set_transient('1_is_1' . $post_id, true, 30);
}
return $data;
}
/**
* This is the action to display the notice in the admin area.
*/
add_action( 'admin_notices', 'display_1_is_1_warning' );
function display_1_is_1_warning() {
global $post;
if ( isset( $post->ID ) && get_transient( '1_is_1' . $post->ID ) ) {
echo '<div id="1_is_1_notice" class="notice notice-success is-dismissible">
<p>1 is 1</p>
</div>';
delete_transient( '1_is_1' . $post->ID );
}
}
/**
* This is to enqueue JavaScript file to handle the display of the notice.
*/
add_action( 'admin_enqueue_scripts', 'enqueue_custom_admin_script' );
function enqueue_custom_admin_script() {
wp_enqueue_script( 'custom-admin-script', get_template_directory_uri() . '/js/custom-admin.js', array( 'jquery' ), null, true );
}
This is the JS Code which we need to added to /js/custom-admin.js
file.
jQuery( document ).ready( function( $ ) {
// Ensure the notice is displayed
var notice = $( '#1_is_1_notice' );
if ( notice.length ) {
notice.closest( '.wrap, .hide-if-js, .block-editor-no-js' ).removeClass( 'hide-if-js block-editor-no-js' );
notice.show();
}
});