admin管理员组

文章数量:1122846

So far I have this action added on pre_post_update, 10, 3

public function check1($post_id, $data){
    if (1==1) {
        set_transient('1_is_1'. $post_id, true, 30);
    }
    return $data;
}

and then I have this action added to admin_notices:

public function display_1_is_1_warning() {
    global $post;

    if (isset($post->ID) && get_transient('1_is_1' . $post->ID)) {
        echo '<div class="notice notice-success is-dismissible">
                <p>1 is 1</p>
              </div>';

        delete_transient('1_is_1' . $post->ID);
    }
}

The HTML of the notice is appended and I can see it's inside a div with classes wrap hide-if-js block-editor-no-js and it's hidden. Is there something I need to do on the JS side to make it appear?

So far I have this action added on pre_post_update, 10, 3

public function check1($post_id, $data){
    if (1==1) {
        set_transient('1_is_1'. $post_id, true, 30);
    }
    return $data;
}

and then I have this action added to admin_notices:

public function display_1_is_1_warning() {
    global $post;

    if (isset($post->ID) && get_transient('1_is_1' . $post->ID)) {
        echo '<div class="notice notice-success is-dismissible">
                <p>1 is 1</p>
              </div>';

        delete_transient('1_is_1' . $post->ID);
    }
}

The HTML of the notice is appended and I can see it's inside a div with classes wrap hide-if-js block-editor-no-js and it's hidden. Is there something I need to do on the JS side to make it appear?

Share Improve this question asked Sep 25, 2024 at 23:12 user18102663user18102663 213 bronze badges 1
  • on my test site, I don't have this wrapper which hide the notice. maybe it's another plugin which interfere with your notice. – mmm Commented Sep 26, 2024 at 10:29
Add a comment  | 

1 Answer 1

Reset to default 0

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();
    }
});

本文标签: