Hook: mam_chat_set_chat_user_name

Signature

apply_filters( 'mam_chat_set_chat_user_name', string $name, int $app_user_id );
Parameter Type Description
$name string The display name the plugin already resolved from WP user data. May be an empty string when the user has no first name, last name, or display name.
$app_user_id int The app_user post ID (not the WordPress user ID) of the participant being named.

Returns: string — the final display name. You must return it.


Purpose

Replaces or augments the way mam-chat-manager resolves a chat participant’s display name. The default resolution chain is:

  1. _billing_first_name user meta (from WooCommerce, if present), otherwise first_name user meta — combined with the corresponding last-name field
  2. The WP user’s display_name
  3. Empty string

The result of that chain becomes the $name argument to this filter. Subscribe to override based on context — vendor business name, role-specific naming, anonymized handles for marketplace-style apps, attribution to the specific admin who replied to a support thread.


When it runs

Inside mam_chat_manager_endpoints::get_display_name() (includes/mam_chat_manager_end_points.php:1216):

$name = apply_filters( 'mam_chat_set_chat_user_name', $name, $app_user_id );
$this->user_name_cache[ $app_user_id ] = $name;
return $name;

The result is cached per request, per app_user_id, in the endpoint instance. The same name will be reused for every subsequent call within the request. If you need the filter to run multiple times per request with different upstream state, your callback has to handle its own freshness — clearing the cache from outside the class is not supported.

get_display_name() is called on every API response that includes participant info: thread list responses, individual thread responses, message responses, and through Hook: mam_add_chat_to_detail on detail-screen responses.


Example: use the vendor business name for sellers

add_filter( 'mam_chat_set_chat_user_name', 'my_app_vendor_business_name', 10, 2 );

function my_app_vendor_business_name( $name, $app_user_id ) {
    $wp_user_id = (int) get_post_meta( $app_user_id, 'user_id', true );
    if ( ! $wp_user_id ) {
        return $name;
    }

    $business = get_user_meta( $wp_user_id, 'vendor_business_name', true );
    if ( ! empty( $business ) ) {
        return $business;
    }

    return $name;
}

Example: anonymize names in a marketplace until first message

add_filter( 'mam_chat_set_chat_user_name', 'my_app_anonymize_pre_contact', 10, 2 );

function my_app_anonymize_pre_contact( $name, $app_user_id ) {
    if ( has_sent_message( $app_user_id ) ) {
        return $name;
    }
    $hash = substr( md5( (string) $app_user_id ), 0, 4 );
    return sprintf( 'Buyer #%s', strtoupper( $hash ) );
}

A common marketplace pattern: hide the user’s real name until they’ve actively messaged at least one seller. Replace has_sent_message() with whatever check makes sense in your data model.


Example: per-admin attribution in support chat

add_filter( 'mam_chat_set_chat_user_name', 'my_app_attribute_admin', 10, 2 );

function my_app_attribute_admin( $name, $app_user_id ) {
    $wp_user_id = (int) get_post_meta( $app_user_id, 'user_id', true );
    if ( ! $wp_user_id ) {
        return $name;
    }

    $user = get_userdata( $wp_user_id );
    if ( $user && in_array( 'administrator', (array) $user->roles, true ) ) {
        // Show the admin's real first name to recipients,
        // overriding the global "Admin" support-chat label.
        $first = get_user_meta( $wp_user_id, 'first_name', true );
        return $first ?: $name;
    }

    return $name;
}

When Recipe: Configure the admin support chat is set up with a single display name (e.g. Acme Support), this filter converts that single label into a per-message attribution like Riley or Sam. Pair with Hook: mam_chat_manager_get_user_avatar if you want each admin’s avatar attributed too.


Gotchas

  • You receive an app_user post ID, not a WP user ID. The two are distinct ID spaces. Convert with get_post_meta( $app_user_id, 'user_id', true ) before reading WP user data.
  • $name may already be empty. Don’t assume there’s a “real” upstream value to fall back to. If the user has no first name, last name, or display_name, you’ll receive ''. Handle the empty case explicitly so participants don’t render as blank.
  • The result is cached for the request. Returning a different value on a second call within the same request has no effect — the first call’s result is reused. If your callback depends on rapidly-changing state (e.g. a draft message in progress), the cache will hide the change.
  • The filter runs everywhere a name is rendered. Be mindful of side effects (database queries, external HTTP calls) in your callback. The thread-list response can resolve dozens of names per request.
  • Returning null produces a runtime error downstream. Always return a string. If you have nothing better to return, return the $name you were passed.

Verification

This article was last verified against:

  • Plugin: mam-chat-manager v2.0.0
  • Source: includes/mam_chat_manager_end_points.php (get_display_name())

Re-verify whenever the resolution chain in get_display_name() changes, the per-request cache is moved out of the endpoint instance, or the filter signature gains additional arguments.


  • Plugin: mam-chat-manager
  • Recipe: Configure the admin support chat — the most common use case for per-admin attribution
  • Hook: mam_chat_manager_get_user_avatar — companion filter for the avatar
  • Hook: mam_add_chat_to_detail — calls get_display_name() when assembling detail payloads

Metadata

Field Value
Article type Hook Reference
Plugin slug mam-chat-manager
Applies to plugin version 2.0.0+
Category Extending MAM Suite
Hook type filter
Audience PHP developer
Last verified 2026-05-01
Contents

    Need Support?

    Can’t find the answer you’re looking for? Don’t worry we’re here to help!