0

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' );
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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.