1

I'm aware that you can pass a custom CSS class name in to an avatar using...

<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>

Like...

get_avatar( $current_user->user_email, 128, null, null, array('class' => array('img-responsive', 'img-rounded') ) );

But I am filtering get_avatar to replace Gravatar with a Media Library image, and the code seems to have rendered the $args portion unfunctional.

Here is my call with the $args class addition...

echo get_avatar($curauth->ID, $size='150', $default='', $alt=$curauth->display_name, $args = array( 'class' => array( 'rounded-circle' ) ) );

But that doesn't add anything.

Here is the filter code...

  /**
   * Use ACF image field as avatar
   * @author Mike Hemberger
   * @link http://thestizmedia.com/acf-pro-simple-local-avatars/
   * @uses ACF Pro image field (tested return value set as Array )
   */
  add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
  function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {

       $user = null; // This added by Robert Andrews to overcome "Notice: Undefined variable" for $user

      // Get user by id or email (get_avatar needs to get a user by either id or email, so account for both)
      if ( is_numeric( $id_or_email ) ) {

          $id   = (int) $id_or_email;
          $user = get_user_by( 'id' , $id );

      } elseif ( is_object( $id_or_email ) ) {

          if ( ! empty( $id_or_email->user_id ) ) {
              $id   = (int) $id_or_email->user_id;
              $user = get_user_by( 'id' , $id );
          }

      } else {
          $user = get_user_by( 'email', $id_or_email );
      }

      if ( ! $user ) {
          return $avatar;
      }

      // Get the user id
      $user_id = $user->ID;

      // Get the file id
      $image_id = get_user_meta($user_id, 'avatar', true); // CHANGE TO YOUR FIELD NAME

      // Bail if we don't have a local avatar
      if ( ! $image_id ) {
          return $avatar;
      }

      // Get the file size
      $image_url  = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
      // Get the file url
      $avatar_url = $image_url[0];
      // Get the img markup
      /* Run through Cloudinary
         cf. http://cloudinary.com/documentation/image_transformations#modify_image_shape_and_style
         crop fill, gravity face, enhance sharpen, 300 wide and tall only to get square image */
      $avatar = '<img alt="' . $alt . '" src="http://res.cloudinary.com/braincloud/image/fetch/w_300,h_300,c_fill,g_face,e_sharpen,b_rgb:ccc/' . $avatar_url . '" class="img-fluid avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';

      // Return our new avatar
      return $avatar;
  }

I have tried added $args to the function declaration, like so...

function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt, $args ) {

... but then the blog page returns: Warning: Missing argument 6 for tsm_acf_profile_avatar() in /home/path/wp-content/plugins/plugin/my-gravatar-replacement-plugin.php on line 26

Someone suggested looking at pluggable.php and copying some relevant code from the get_avatar part there, but I don't understand.

How do I restore $args functionality so I can continue adding custom classes to my avatars?

2

If you look at the documentation for teh get_avatar() filter, you'll see that callbacks can accept 6 arguments:

apply_filters( 'get_avatar', string $avatar, mixed $id_or_email, int $size, string $default, string $alt, array $args )

Which means that your callback function is accepting the arguments correctly, including the 6th argument, $args:

function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {

The problem is that $args isn't being passed into your callback function. This is because add_filter() only passes the number of arguments specified in the last argument, $accepted_args. See the documentation:

$accepted_args

(int) (Optional)

The number of arguments the function accepts. Default value: 1

This is basically a long-winded way of saying that you need to change 5 to 6 in your add_filter() call:

add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 6);
  • Ah. So if I modify these like: add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 6); function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt, $args ) { and get_avatar passes: $args = array( 'class' => array( 'rounded-circle' ) ) ; … that should work, is that right? Seeing nothing, but must echo class out here?: $avatar = '<img alt="' . $alt . '" src="$avatar_url . '" class="img-fluid avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>'; … I see where, but I don’t know how to access the “class” arg. – Robert Andrews Nov 30 '17 at 11:31
  • You need to get the class from $args['class'], but you should check it exists first (isset( $args['class'])) – Jacob Peattie Nov 30 '17 at 12:34
  • How to overcome warning Notice: Array to string conversion ... when I write $args['class'] in to the img mark-up? – Robert Andrews Nov 30 '17 at 12:46
  • $args['class'] can be an array or a string, you need to account for both. Simplest solution is just to cast as an array then implode it: $class = (array) $args['class']; echo implode( ' ', $class ); – Jacob Peattie Nov 30 '17 at 12:52
  • You should look at the source of get_avatar() to see how it handles all the arguments. – Jacob Peattie Nov 30 '17 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.