Using WordPress ‘get_avatar_data’ PHP filter

The get_avatar_data WordPress PHP filter allows you to modify the avatar data before it is retrieved.

Usage

add_filter('get_avatar_data', 'your_custom_function', 10, 2);
function your_custom_function($args, $id_or_email) {
  // your custom code here
  return $args;
}

Parameters

  • $args (array) – Arguments passed to get_avatar_data(), after processing.
  • $id_or_email (mixed) – The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.

More information

See WordPress Developer Resources: get_avatar_data

Examples

Change default avatar

Change the default avatar to a custom image.

add_filter('get_avatar_data', 'change_default_avatar', 10, 2);
function change_default_avatar($args, $id_or_email) {
  $args['default'] = 'https://example.com/images/custom-avatar.png';
  return $args;
}

Force avatar size

Force a specific avatar size, regardless of the size requested.

add_filter('get_avatar_data', 'force_avatar_size', 10, 2);
function force_avatar_size($args, $id_or_email) {
  $args['size'] = 100;
  return $args;
}

Use local avatar for a specific user

Use a local avatar image for a specific user based on their email address.

add_filter('get_avatar_data', 'use_local_avatar', 10, 2);
function use_local_avatar($args, $id_or_email) {
  if ($id_or_email === '[email protected]') {
    $args['url'] = 'https://example.com/images/local-avatar.jpg';
  }
  return $args;
}

Disable Gravatar use

Disable Gravatar use and fallback to the default avatar.

add_filter('get_avatar_data', 'disable_gravatar', 10, 2);
function disable_gravatar($args, $id_or_email) {
  $args['url'] = false;
  return $args;
}

Add custom class to avatar

Add a custom class to the avatar image.

add_filter('get_avatar_data', 'add_custom_avatar_class', 10, 2);
function add_custom_avatar_class($args, $id_or_email) {
  $args['class'] = 'custom-avatar-class';
  return $args;
}