Skip to main content

Just curious...

Is there a way to set my website with my Community site using FC that will pull the user information/data from FC and update the user in the WordPress backend admin dashboard?

Right now I'm seeing full profiles with profile pics (avatars) on the Community side, but in the backend under "Users" all of the pictures and some other relevant information is missing.

Is there any way to get these two user spaces synced?

Thanks.

Settings > Privacy > Sync Community Profile with WordPress User Data

David Fore

Tawsif Ahmed RiyadΒ I have that option checked, but it isn't syncing the user's profile pic. It may sync some of the information (data) but not the pic. Shouldn't it sync all information including the profile pic, and change it if the member changes it?

David Fore, use the below snippet:

add_filter('pre_get_avatar_data', function ($args, $id_or_email) {
    $userId = null;
    if (is_numeric($id_or_email)) {
        $userId = (int) $id_or_email;
    } elseif ($id_or_email instanceof \WP_User) {
        $userId = $id_or_email->ID;
    } elseif ($id_or_email instanceof \WP_Post) {
        $userId = (int) $id_or_email->post_author;
    }

    if (!$userId) {
        return $args;
    }

    $photo = get_user_meta($userId, '_fcom_user_photo', true);
    if ($photo) {
        $args['url'] = $photo;
    }

    return $args;
}, 10, 2);

Check your WP admin user directory; it will show the community profile photos.

David Fore

Tawsif Ahmed RiyadΒ Ok, thanks. Is this a PHP snippet? or something else, cause I created a new PHP snippet using FluentSnippets and it doesn't seem to be working. Even after clearing all of my cache a few times. πŸ€”

David Fore, php snippet. Did you activate the snippet?

David Fore

Tawsif Ahmed RiyadΒ Yes sir. It's activated. In the second screenshot below, the only avatars (Profile pics) that you see are generated from gravatar.

David Fore, I see, my snippet won't work without core modification. You may check this:
https://community.wpmanageninja.com/portal/post/push-community-profile-photo-to-user-pro?comment_id=31721

David Fore

Tawsif Ahmed RiyadΒ Yes, that one worked.

This is the one that worked for me:

/**
 * Replace WordPress avatar with FluentCommunity profile picture
 */
add_filter('get_avatar_url', 'use_fluentcommunity_avatar', 10, 3);

function use_fluentcommunity_avatar($url, $id_or_email, $args) {
    global $wpdb;

    // Get user ID from various input types
    $user_id = null;
    if (is_numeric($id_or_email)) {
        $user_id = (int) $id_or_email;
    } elseif (is_object($id_or_email) && isset($id_or_email->user_id)) {
        $user_id = (int) $id_or_email->user_id;
    } elseif (is_string($id_or_email)) {
        $user = get_user_by('email', $id_or_email);
        if ($user) {
            $user_id = $user->ID;
        }
    }

    // If we couldn't get a user ID, return the original URL
    if (!$user_id) {
        return $url;
    }

    // Query the FluentCommunity database table
    $table_name = $wpdb->prefix . 'fcom_xprofile';
    $avatar = $wpdb->get_var($wpdb->prepare(
        "SELECT avatar FROM $table_name WHERE user_id = %d",
        $user_id
    ));

    // If FluentCommunity avatar exists and is not empty, use it
    if ($avatar && !empty($avatar)) {
        return $avatar;
    }

    // Otherwise, return the original WordPress avatar
    return $url;
}