我在订阅者个人资料屏幕上添加了一个管理员通知,邀请他们填写个人资料的额外字段。当用户更新配置文件时,通知如何自动隐藏?当用户更新配置文件时,第一个配置文件下方会出现一个新通知,但邀请仍然存在。我正在使用此代码。
function wpse_user_welcome_notice() {
// Make sure that the user is assigned to the subscriber role, specifically.
$user = wp_get_current_user();
if ( !in_array( \'subscriber\', $user->roles ) ) {
return;
}
// Make sure the profile is being viewed.
$screen = get_current_screen();
if (!$screen || ( \'profile\' !== $screen->id ) ) {
return;
}
$class = \'notice notice-info is-dismissible\';
// Customize the HTML to fit your preferences.
$message = \'<p>Example text goes here.. </a></p>\';
printf( \'<div class="%1$s"><div class="subscriberProfile">%2$s</div></div>\',
$class, $message );
}
add_action( \'admin_notices\', \'wpse_user_welcome_notice\' );
最合适的回答,由SO网友:James 整理而成
您可以简单地检查每个用户元详细信息,看看它们是否为空。下面的代码是您的,带有名字和姓氏的检查。
function wpse_user_welcome_notice() {
// Make sure that the user is assigned to the subscriber role, specifically.
$user = wp_get_current_user();
if ( !in_array( \'subscriber\', $user->roles ) ) { return; }
// Make sure the profile is being viewed.
$screen = get_current_screen();
if (!$screen || ( \'profile\' !== $screen->id ) ) { return; }
// CHECK IF FIELDS ARE FILLED IN
$current_user = wp_get_current_user();
if(!$current_user->user_firstname && !$current_user->user_lastname)){
$class = \'notice notice-info is-dismissible\';
// Customize the HTML to fit your preferences.
$message = \'<p>Example text goes here.. </a></p>\';
printf( \'<div class="%1$s"><div class="subscriberProfile">%2$s</div></div>\',
$class, $message );
}
}
add_action( \'admin_notices\', \'wpse_user_welcome_notice\' );