Halloween Sale 15% off Ends in See the plans
PHP

BuddyPress: Remind Members to Complete Their Profile

Shows a friendly notice on BuddyPress pages to members who have not filled in important profile fields or uploaded a profile photo, with a link to edit their profile.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! function_exists( 'buddypress' ) ) {
    return;
}

// Profile field names that should be filled in.
$wpide_bp_required_fields = array( 'Location', 'About me' );

add_action( 'bp_before_member_header', function () use ( $wpide_bp_required_fields ) {
    $user_id = bp_loggedin_user_id();

    if ( ! $user_id || ! bp_is_my_profile() || ! bp_is_active( 'xprofile' ) || bp_is_user_profile_edit() ) {
        return;
    }

    $missing = get_transient( 'wpide_bp_missing_' . $user_id );

    if ( false === $missing ) {
        $missing = array();

        foreach ( $wpide_bp_required_fields as $field ) {
            if ( '' === trim( (string) xprofile_get_field_data( $field, $user_id, 'comma' ) ) ) {
                $missing[] = $field;
            }
        }

        if ( ! bp_get_user_has_avatar( $user_id ) ) {
            $missing[] = 'profile photo';
        }

        set_transient( 'wpide_bp_missing_' . $user_id, $missing, HOUR_IN_SECONDS );
    }

    if ( $missing ) {
        printf(
            '<div class="bp-feedback info wpide-bp-complete"><span class="bp-icon" aria-hidden="true"></span><p>%1$s <a href="%2$s">Complete your profile</a></p></div>',
            esc_html( sprintf( 'Your profile is missing: %s.', implode( ', ', $missing ) ) ),
            esc_url( trailingslashit( function_exists( 'bp_members_get_user_url' ) ? bp_members_get_user_url( $user_id ) : bp_core_get_user_domain( $user_id ) ) . 'profile/edit/' )
        );
    }
} );

add_action( 'xprofile_updated_profile', function ( $user_id ) {
    delete_transient( 'wpide_bp_missing_' . (int) $user_id );
} );
add_action( 'bp_members_avatar_uploaded', function ( $item_id ) {
    delete_transient( 'wpide_bp_missing_' . (int) $item_id );
} );

Notes

List the field names to check in $wpide_bp_required_fields. The result is cached per member for an hour.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.