SHARE
TWEET

bp_fetch_avatar_url_filter

elhardoum Jan 23rd, 2017 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Replace default WordPress avatars URL with BP avatars URL, if available.
  3.  *
  4.  * See 'get_avatar_url' filter description in wp-includes/pluggable.php.
  5.  *
  6.  * @param string            $url            The avatar path passed to 'get_avatar'.
  7.  * @param int|string|object $id_or_email    A user ID, email address, or comment object.
  8.  * @param array             $args           Arguments passed to get_avatar_data(), after processing.
  9.  * @return string BP avatar URL, if found; else the original avatar URL.
  10.  */
  11. function bp_fetch_avatar_url_filter( $url, $id_or_email, $args ) {
  12.     /**
  13.       * Remove this filter to prevent infinite loop as
  14.       * get_avatar itself calls get_avatar_url
  15.       */
  16.     remove_filter('get_avatar_url', __FUNCTION__, 11, 3);
  17.  
  18.     // get avatar HTML
  19.     $avatar_html = call_user_func_array('get_avatar', array(
  20.         $id_or_email,
  21.         isset($args['size']) ? esc_attr($args['size']) : null,
  22.         isset($args['default']) ? esc_attr($args['default']) : null,
  23.         null,
  24.         $args
  25.     ));
  26.  
  27.     /**
  28.       * Restore back the filter now that we got the avatar HTML
  29.       */
  30.     add_filter('get_avatar_url', __FUNCTION__, 11, 3);
  31.  
  32.     /** Search for image src attribute // regex might need improvement **/
  33.     preg_match('/<img\s*(.*?) src=["\']?(.*?)["\']? (.*?)>/si', $avatar_html, $src);
  34.    
  35.     /** assign the image src as the avatar URL **/
  36.     if ( !empty( $src[2] ) ) {
  37.         $url = esc_url( $src[2] );
  38.     }
  39.  
  40.     /** the new avatar URL, bp filtered **/
  41.     return $url;
  42. }
  43. add_filter( 'get_avatar_url', 'bp_fetch_avatar_url_filter', 11, 3 );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top