I added an admin notice to the subscribers profile screen to invite them to complete their profile's extra fields. How can the notice hide automatically when the user updates the profile? When a user updates the profile a new notice appears below the first one but the the invitation remains. Im using this code.
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' );
I added an admin notice to the subscribers profile screen to invite them to complete their profile's extra fields. How can the notice hide automatically when the user updates the profile? When a user updates the profile a new notice appears below the first one but the the invitation remains. Im using this code.
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' );
Share
Improve this question
asked Apr 12, 2018 at 23:14
ArtusArtus
929 bronze badges
1 Answer
Reset to default 1You can simply check each of user meta details to see if they are blank or not. The code below is yours with with a check for first and last name.
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' );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745126816a4612739.html
评论列表(0条)