• 1.
     
  • 2.
    /**
  • 3.
     * Replace Gravatar with custom image URL in wp_usermeta - photo_url
  • 4.
     * cf. Mike Hemberger http://thestizmedia.com/acf-pro-simple-local-avatars/ and https://twitter.com/JiveDig/status/706138742604570624
  • 5.
    */
  • 6.
     
  • 7.
    add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
  • 8.
    function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
  • 9.
     
  • 10.
          // Get user by id or email
  • 11.
          if ( is_numeric( $id_or_email ) ) {
  • 12.
              $id   = (int) $id_or_email;
  • 13.
              $user = get_user_by( 'id' , $id );
  • 14.
          } elseif ( is_object( $id_or_email ) ) {
  • 15.
              if ( ! empty( $id_or_email->user_id ) ) {
  • 16.
                  $id   = (int) $id_or_email->user_id;
  • 17.
                  $user = get_user_by( 'id' , $id );
  • 18.
              }
  • 19.
          } else {
  • 20.
              $user = get_user_by( 'email', $id_or_email );
  • 21.
          }
  • 22.
          // Get the user id
  • 23.
          $user_id = $user->ID;
  • 24.
     
  • 25.
     
  • 26.
      // Get the file id
  • 27.
      $image_url = get_user_meta($user_id, 'photo_url', true); // CHANGE TO YOUR FIELD NAME
  • 28.
      // Bail if we don't have a local avatar
  • 29.
      if ( ! $image_url ) {
  • 30.
          return $avatar;
  • 31.
      }
  • 32.
     
  • 33.
      // Get the img markup
  • 34.
      $avatar = '<img alt="' . $alt . '" src="' . $image_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
  • 35.
      // Return our new avatar
  • 36.
      return $avatar;
  • 37.
     
  • 38.
     
  • 39.
    }
  • 40.
     
/** * Replace Gravatar with custom image URL in wp_usermeta - photo_url * cf. Mike Hemberger http://thestizmedia.com/acf-pro-simple-local-avatars/ and https://twitter.com/JiveDig/status/706138742604570624 */ add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5); function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) { // Get user by id or email 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 ); } // Get the user id $user_id = $user->ID; // Get the file id $image_url = get_user_meta($user_id, 'photo_url', true); // CHANGE TO YOUR FIELD NAME // Bail if we don't have a local avatar if ( ! $image_url ) { return $avatar; } // Get the img markup $avatar = '<img alt="' . $alt . '" src="' . $image_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>'; // Return our new avatar return $avatar; }

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter