Hide admin notice when user profile updated

I added an admin notice to the subscribers profile screen to invite them to complete their profile's extra fields.

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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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

相关推荐

  • Hide admin notice when user profile updated

    I added an admin notice to the subscribers profile screen to invite them to complete their profile's extra fields.

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信