How to sync user profile picture with WordPress user profile picture

I’m trying to make sure that when a user (or vendor) uploads a profile picture in HivePress, the same image is automatically used as their WordPress user profile picture.

The reason I want this is:

  • The WordPress user profile image is also used for blog author avatars and other pages outside HivePress.
  • Right now, WordPress is falling back to Gravatar even if the user has already uploaded a profile picture while signing up.
  • As the signup process is completely done by Hivepress, and the user settings are also done from the HivePress Dashboard.

Could you please guide me on how to properly hook into HivePress so that the uploaded HivePress profile picture syncs with the WordPress user profile picture?

I was able to achieve this using this code. Maybe it will be helpful for someone else.

// Always use HivePress Vendor image as WordPress avatar if available
add_filter( 'get_avatar_url', function( $url, $id_or_email, $args ) {
    $user = false;

    // Detect WP user
    if ( is_numeric( $id_or_email ) ) {
        $user = get_user_by( 'id', absint( $id_or_email ) );
    } elseif ( $id_or_email instanceof WP_User ) {
        $user = $id_or_email;
    } elseif ( $id_or_email instanceof WP_Post ) {
        $user = get_user_by( 'id', (int) $id_or_email->post_author );
    } elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
        $user = get_user_by( 'id', (int) $id_or_email->user_id );
    } elseif ( is_string( $id_or_email ) ) {
        $user = get_user_by( 'email', $id_or_email );
    }

    if ( $user && function_exists( 'hivepress' ) ) {
        // Find vendor profile linked to this user
        $vendor = \HivePress\Models\Vendor::query()
            ->filter( ['user' => $user->ID] )
            ->get_first();

        if ( $vendor ) {
            $image_id = $vendor->get_image__id();
            if ( $image_id ) {
                $img_url = wp_get_attachment_image_url( $image_id, [ $args['size'], $args['size'] ] );
                if ( $img_url ) {
                    return $img_url;
                }
            }
        }
    }

    // Default: return Gravatar or WP fallback
    return $url;
}, 10, 3 );```
1 Like

Hi,

Thank you for your solution, it will be useful for our community.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.