有两种处理方法。
a. Attach a timer to the notice:
您可以在通知上附加一个3秒计时器(或任意长的计时器),如下所示:
<?php
set_transient( "my-plugin", "alive", 3 );
add_action( \'admin_notices\', function() {
//Syntax correction
if ( "alive" == get_transient( "my-plugin" ) ) {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( \'Imagine something here!\', \'sample-text-domain\' ); ?></p>
</div>
<?php
delete_transient( "my-plugin" );
}
});
3秒钟后,瞬态将过期,并且将来对
admin_notices
挂钩函数不会显示通知。插件安装完成后,应该有足够的时间在第一页刷新时显示通知。您可以根据需要使用计时器。
b. Keep a history of the dismissal of the notice, with your own dismiss action:
function my_plugin_notice() {
$user_id = get_current_user_id();
if ( !get_user_meta( $user_id, \'my_plugin_notice_dismissed\' ) )
echo \'<div class="notice"><p>\' . _e( \'Imagine something here!\', \'sample-text-domain\' ) . \'</p><a href="?my-plugin-dismissed">Dismiss</a></div>\';
}
add_action( \'admin_notices\', \'my_plugin_notice\' );
function my_plugin_notice_dismissed() {
$user_id = get_current_user_id();
if ( isset( $_GET[\'my-plugin-dismissed\'] ) )
add_user_meta( $user_id, \'my_plugin_notice_dismissed\', \'true\', true );
}
add_action( \'admin_init\', \'my_plugin_notice_dismissed\' );
一旦解除,用户元将使用该信息更新,并且
admin_notices
钩子函数将对此进行检查,并且仅在不正确时显示消息。