不需要库,它很容易实现。如果您正在编写自己的插件(看起来您是),那么您可以以一种相当轻量级的方式来完成这项工作:
通知的略微修改版本,在显示之前检查通知是否已被取消,并将通知的“类型”存储在标记中,以便在javascript中访问:
function prefix_deprecated_hook_admin_notice() {
if ( has_filter( \'prefix_filter\' ) ) {
// Check if it\'s been dismissed...
if ( ! get_option(\'dismissed-prefix_deprecated\', FALSE ) ) {
// Added the class "notice-my-class" so jQuery pick it up and pass via AJAX,
// and added "data-notice" attribute in order to track multiple / different notices
// multiple dismissible notice states ?>
<div class="updated notice notice-my-class is-dismissible" data-notice="prefix_deprecated">
<p><?php _e( \'Notice\', \'my-text-domain\' ); ?></p>
</div>
<?php }
}
}
add_action( \'admin_notices\', \'prefix_deprecated_hook_admin_notice\' );
然后,下面的jQuery
// shorthand no-conflict safe document-ready function
jQuery(function($) {
// Hook into the "notice-my-class" class we added to the notice, so
// Only listen to YOUR notices being dismissed
$( document ).on( \'click\', \'.notice-my-class .notice-dismiss\', function () {
// Read the "data-notice" information to track which notice
// is being dismissed and send it via AJAX
var type = $( this ).closest( \'.notice-my-class\' ).data( \'notice\' );
// Make an AJAX call
// Since WP 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.ajax( ajaxurl,
{
type: \'POST\',
data: {
action: \'dismissed_notice_handler\',
type: type,
}
} );
} );
});
在PHP中,设置AJAX挂钩以捕获AJAX调用:
add_action( \'wp_ajax_dismissed_notice_handler\', \'ajax_notice_handler\' );
/**
* AJAX handler to store the state of dismissible notices.
*/
function ajax_notice_handler() {
// Pick up the notice "type" - passed via jQuery (the "data-notice" attribute on the notice)
$type = self::request( \'type\' );
// Store it in the options table
update_option( \'dismissed-\' . $type, TRUE );
}
就这样!不需要库-特别是如果您已经加载了PHP和javascript文件。
此方法符合WordPress Core dev team blog