Using WordPress ‘default_avatar_select’ PHP filter

The default_avatar_select WordPress PHP filter allows you to modify the HTML output of the default avatar list.

Usage

add_filter('default_avatar_select', 'your_custom_function');
function your_custom_function($avatar_list) {
  // Your custom code here
  return $avatar_list;
}

Parameters

  • $avatar_list (string): HTML markup of the avatar list.

More information

See WordPress Developer Resources: default_avatar_select

Examples

Add a custom avatar to the list

Add a new custom avatar option to the default avatar list.

add_filter('default_avatar_select', 'add_custom_avatar');
function add_custom_avatar($avatar_list) {
  $custom_avatar = '<input type="radio" id="custom" name="avatar_default" value="custom_avatar_url">';
  $custom_avatar .= '<label for="custom">Custom Avatar</label><br />';

  return $avatar_list . $custom_avatar;
}

Remove the ‘Mystery Person’ avatar option

Remove the ‘Mystery Person’ avatar option from the default avatar list.

add_filter('default_avatar_select', 'remove_mystery_person_avatar');
function remove_mystery_person_avatar($avatar_list) {
  return str_replace('<label for="mystery">Mystery Person</label><br />', '', $avatar_list);
}

Add custom CSS class to avatar options

Add a custom CSS class to each avatar option in the list.

add_filter('default_avatar_select', 'add_css_class_to_avatars');
function add_css_class_to_avatars($avatar_list) {
  return str_replace('<input', '<input class="custom-class"', $avatar_list);
}

Rearrange avatar options

Move the Gravatar logo option to the end of the avatar list.

add_filter('default_avatar_select', 'rearrange_avatar_options');
function rearrange_avatar_options($avatar_list) {
  $gravatar_logo = '<input type="radio" id="gravatar_logo" name="avatar_default" value="gravatar_logo">';
  $gravatar_logo .= '<label for="gravatar_logo">Gravatar Logo</label><br />';

  $avatar_list = str_replace($gravatar_logo, '', $avatar_list);
  return $avatar_list . $gravatar_logo;
}

Wrap avatar options in a div

Wrap the avatar options in a div with a custom class.

add_filter('default_avatar_select', 'wrap_avatar_options');
function wrap_avatar_options($avatar_list) {
  return '<div class="custom-wrapper">' . $avatar_list . '</div>';
}