Skip to main content

benecaster_avatar_fallback

Filter Free

Filters the avatar URL used when a subscriber has no uploaded avatar and site-wide Gravatar is disabled. Return a fully qualified URL to serve a custom fallback image — for example, an initials-based avatar generated from the subscriber’s display name. Return `null` to let WordPress fall through to its own default avatar (typically the ‘mystery person’ silhouette).

This filter is bypassed entirely when site-wide Gravatar is enabled. In that case, WordPress’s Gravatar URL is used without calling this filter.

Parameters

Name Type Default Description
$url ?string Current fallback URL — null by default
$user_id int WordPress user ID

Examples

Serve initials-based avatar as fallback

add_filter( 'benecaster_avatar_fallback', function ( ?string $url, int $user_id ): ?string {
    $user = get_userdata( $user_id );
    if ( ! $user ) {
        return $url;
    }
    $initial = strtoupper( mb_substr( $user->display_name ?: $user->user_email, 0, 1 ) );
    // Return a URL to your initials image generator service
    return 'https://example.com/avatars/initials?letter=' . urlencode( $initial );
}, 10, 2 );