Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use pre_get_avatar_data filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 30, 2022
5 minutes read

pre_get_avatar_data filter

Passing a non-null value in the ‘url’ member of the return array will effectively short circuit get_avatar_data(), passing the value through the ‘get_avatar_data’ filter and returning early.

To use the pre_get_avatar_data filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

At WePlugins, we always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live examples, we have defined a function modify_pre_get_avatar_data_defaults which takes 2 parameters and we registered using add_filter. The first parameter pre_get_avatar_data is the name of the hook, the second parameter modify_pre_get_avatar_data_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove the pre_get_avatar_data filter.

Parameters

Below are the 2 parameters required to use this hook.

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

Live Example 1

Here’s how you can use the pre_get_avatar_data filter to customize the avatar URL based on specific conditions:

add_filter( 'pre_get_avatar_data', function( $data, $id_or_email ) {
      if ( 'author_email_to_check' == $id_or_email || 'author_id_to_check' == $id_or_email ) {
          $img_src = wp_get_attachment_image_src( 'attachment_id_to_display', array( $data['width'], $data['height'] ) );
          if ( isset( $img_src[0] ) ) {
              $data['url'] = $img_src[0];
          }
      }
      return $data;
  }, 10, 2 );

Live Example 2

Below is an example of how you can modify the arguments passed to get_avatar_data:

  function weplugins_modify_pre_get_avatar_data_defaults($args, $id_or_email) {
      // Update the $args variable according to your website requirements and return this variable. You can modify the $args variable conditionally too if you want.
      return $args;
  }
  // add the filter
  add_filter( "pre_get_avatar_data", "weplugins_modify_pre_get_avatar_data_defaults", 10, 2 );
  

Live Example 3

To remove a hook callback, use the example below.

remove_filter( "pre_get_avatar_data", "weplugins_modify_pre_get_avatar_data_defaults", 10, 2 );

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need any customization or help with this hook, feel free to contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.