Skip to main content

Render an initials-based avatar when site-wide Gravatar is disabled

Premium Beginner

A subscriber who has not uploaded a photo, on a site with Gravatar switched off, has no picture at all — and WordPress’s stock placeholder is not something most podcasters want on their Supporter Wall.

benecaster_avatar_fallback fires in exactly that gap and lets you supply something better, such as a generated initials image in the show’s colours. Decline to handle it and WordPress’s own default is used, so you can substitute for some subscribers and leave the rest alone.

It only runs when Gravatar is off site-wide. With Gravatar enabled, that is the fallback and this never fires — so test with the setting in the state your podcasters actually use, or you will be testing a code path that never runs for them.

Code

<?php
add_filter( 'benecaster_avatar_fallback', function ( ?string $url, int $user_id ): ?string {
    if ( $user_id <= 0 ) {
        return $url;
    }
    $user = get_userdata( $user_id );
    if ( ! $user ) {
        return $url;
    }
    $initial = strtoupper( substr( $user->display_name ?: $user->user_login, 0, 1 ) );
    return 'https://avatars.my-cdn.example/initials/' . rawurlencode( $initial ) . '.png';
}, 10, 2 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →