Using WordPress ‘avatar_defaults’ PHP filter

The avatar_defaults WordPress PHP filter allows you to modify the list of default avatars available in WordPress.

Usage

add_filter('avatar_defaults', 'my_custom_avatars');
function my_custom_avatars($avatar_defaults) {
  // Your custom code here
  return $avatar_defaults;
}

Parameters

  • $avatar_defaults (string[]): An associative array of default avatars, where the key is the option value, and the name is the displayed avatar name.

More information

See WordPress Developer Resources: avatar_defaults

Examples

Add a custom avatar

This example adds a custom avatar to the default avatars list.

add_filter('avatar_defaults', 'my_custom_avatars');
function my_custom_avatars($avatar_defaults) {
  $my_avatar = 'https://example.com/path/to/my-avatar.png';
  $avatar_defaults[$my_avatar] = 'My Custom Avatar';
  return $avatar_defaults;
}

Remove a default avatar

This example removes the ‘Mystery Person’ avatar from the default avatars list.

add_filter('avatar_defaults', 'remove_mystery_person_avatar');
function remove_mystery_person_avatar($avatar_defaults) {
  unset($avatar_defaults['mystery']);
  return $avatar_defaults;
}

Change the display name of a default avatar

This example changes the display name of the ‘Gravatar Logo’ avatar.

add_filter('avatar_defaults', 'change_gravatar_logo_name');
function change_gravatar_logo_name($avatar_defaults) {
  $avatar_defaults['gravatar_default'] = 'New Gravatar Logo Name';
  return $avatar_defaults;
}

Sort default avatars alphabetically

This example sorts the default avatars list alphabetically by their display names.

add_filter('avatar_defaults', 'sort_avatars_alphabetically');
function sort_avatars_alphabetically($avatar_defaults) {
  asort($avatar_defaults);
  return $avatar_defaults;
}

Add multiple custom avatars

This example adds multiple custom avatars to the default avatars list.

add_filter('avatar_defaults', 'add_multiple_custom_avatars');
function add_multiple_custom_avatars($avatar_defaults) {
  $avatar1 = 'https://example.com/path/to/avatar1.png';
  $avatar2 = 'https://example.com/path/to/avatar2.png';

  $avatar_defaults[$avatar1] = 'Custom Avatar 1';
  $avatar_defaults[$avatar2] = 'Custom Avatar 2';

  return $avatar_defaults;
}