Skip to main content

Username in FC does not reflect username in WP

When someone changes username on this FC profile, why it is not used also for Wordpress? It is not username but display name then?

Username in FC does not reflect username in WP Username in FC does not reflect username in WP

Drive Zone

Problem is that I use some snippets with username but they stop working when user changes his username on FC profile and snippet keep linking on WP username

Ross FCA

Drive ZoneΒ by default the FC display name is users FirstName+LastName - there's a function that runs after every time the user updates their profile that reapplies this formula to the display name (for cases where you update either your first or last name).

If you want a custom display name you need to hook in and change that profile save function, or overwrite it. My current solution takes a new pattern, applies it to every user in bulk, then adds a rewrite to the user-save-profile function to apply my formula to the display name. Works mostly and the only issue so far is a quick flash of the original display name for the user when they save their profile.

Tawsif Ahmed Riyad

WordPress doesΒ notΒ allow changing theΒ user_loginΒ (username) from the admin dashboard or viaΒ wp_update_user(). It is designed to be immutable after the user is created.

The username in Fluent Community ($xprofile->username) is a separate field and can be changed.

The WordPress core username (user_loginΒ inΒ wp_users) cannot be changed after account creation.

https://developer.wordpress.org/reference/functions/wp_update_user/

https://stackoverflow.com/questions/46972779/unable-to-update-an-username-in-wordpress

https://wordpress.stackexchange.com/questions/135304/why-cant-i-update-username-through-wordpress-api

Drive Zone

Tawsif Ahmed RiyadΒ Any idea what to change in this snippet to link on correct user URL? After username change it keep linking on old username.

/**

  • MEC seznam účastnΓ­kΕ― – avatar z Fluent Community (fallback na Gravatar)
    */
    add_action('mec_single_after_content', function($event) {

    if (!class_exists('MEC_main')) return;

    $MEC = MEC::getInstance('app.libraries.main');
    $book = $MEC->getBook();

    $date = $event->date;
    $timestamp = isset($date['start']['timestamp']) ? $date['start']['timestamp'] : current_time('timestamp');

    // ZΓ­skΓ‘nΓ­ vΕ‘ech rezervacΓ­ pro danΓ½ event
    $bookings = $MEC->get_bookings($event->data->ID, $timestamp, 100);

    if (!$bookings || empty($bookings)) {
    echo '

    ℹ️ Ε½Γ‘dnΓ­ členovΓ© zatΓ­m nepotvrdili účast na tΓ©to udΓ‘losti. Buď prvnΓ­!

    ';
    return;
    }

    echo '';
    echo '

    Účastníci

    ';
    echo '
      ';

      foreach ($bookings as $booking) {
      $author_id = $booking->post_author;
      $user = get_userdata($author_id);

       $name = $user ? $user->display_name : __('NeznΓ‘mΓ½ uΕΎivatel', 'mec');
       $total_attendees = $book->get_total_attendees($booking->ID);
       $profile_url = $user ? site_url('/portal/u/' . $user->user_nicename) : '#';
      
       // Fluent avatar fallback
       $avatar_url = drivezone_get_fluent_avatar($author_id);
       if (!$avatar_url) {
           $avatar_url = get_avatar_url($author_id, ['size' => 64]);
       }
      
       echo '<li class="mec-attendee-item" data-user-id="' . esc_attr($author_id) . '">';
       echo '<div class="mec-attendee-avatar">';
       echo '<a href="' . esc_url($profile_url) . '" class="mec-attendee-link">';
       echo '<img src="' . esc_url($avatar_url) . '" alt="' . esc_attr($name) . '" width="64" height="64" class="mec-avatar-img" style="border-radius:50%;object-fit:cover;">';
       echo '</a>';
       echo '</div>';
       echo '<div class="mec-attendee-info">';
       echo '<a href="' . esc_url($profile_url) . '" class="mec-attendee-name"><strong>' . esc_html($name) . '</strong></a><br>';
       echo '<span class="mec-attendee-count">' . sprintf(_n('%s lΓ­stek', '%s lΓ­stkΕ―', $total_attendees, 'mec'), $total_attendees) . '</span>';
       echo '</div>';
       echo '</li>';

      }

      echo '

    ';
    });

Tawsif Ahmed Riyad

Drive Zone, for $profile_url you are using $user->user_nicename, which is not the username of a user. Try this out and let me know.

global $wpdb;  // add this before the foreach loop
foreach ($bookings as $booking) {
    $author_id = $booking->post_author;
    $user = get_userdata($author_id);

    // Get username from wp_fcom_xprofile
    $fluent_community_username = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT username FROM {$wpdb->prefix}fcom_xprofile WHERE user_id = %d LIMIT 1",
            $author_id
        )
    );

// rest of the code

$profile_url = $fluent_community_username ? site_url('/portal/u/' . $fluent_community_username) : '#';  // updated;; used $fluent_community_username for valid profile url

// rest of the code

You need to query it using theΒ user_id of wp_fcom_xprofile table to retrieve the correct/current username of a user.

Drive Zone

Tawsif Ahmed RiyadΒ Works! Thank you!