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?
- 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
1 Answer
Reset to default 0You 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();
}
});
本文标签:
版权声明:本文标题:admin - I want to display a notification in editor when user saves a post , when certain criteria are met 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288292a1928064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论