2

I'm using basic user avatars for use local avatar in wordpress. And i have a simple function in my functions.php for getting avatar direct link. My problem is if i use get_avatar function in my functions php it returns gravatar img but if i use get_avatar function in my frontend pages it returns local avatars. Here is plugins's code

public function __construct() {

    // Text domain
    $this->load_textdomain();

    // Actions
    add_action( 'admin_init',                array( $this, 'admin_init'               )        );
    add_action( 'show_user_profile',         array( $this, 'edit_user_profile'        )        );
    add_action( 'edit_user_profile',         array( $this, 'edit_user_profile'        )        );
    add_action( 'personal_options_update',   array( $this, 'edit_user_profile_update' )        );
    add_action( 'edit_user_profile_update',  array( $this, 'edit_user_profile_update' )        );
    add_action( 'bbp_user_edit_after_about', array( $this, 'bbpress_user_profile'     )        );

    // Shortcode
    add_shortcode( 'basic-user-avatars',     array( $this, 'shortcode'                )        );

    // Filters
    add_filter( 'get_avatar',                array( $this, 'get_avatar'               ), 10, 5 );
    add_filter( 'avatar_defaults',           array( $this, 'avatar_defaults'          )        );
}

public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {

    // Determine if we recive an ID or string
    if ( is_numeric( $id_or_email ) )
        $user_id = (int) $id_or_email;
    elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
        $user_id = $user->ID;
    elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
        $user_id = (int) $id_or_email->user_id;

    if ( empty( $user_id ) )
        return $avatar;

    $local_avatars = get_user_meta( $user_id, 'basic_user_avatar', true );

    if ( empty( $local_avatars ) || empty( $local_avatars['full'] ) )
        return $avatar;

    $size = (int) $size;

    if ( empty( $alt ) )
        $alt = get_the_author_meta( 'display_name', $user_id );

    $author_class = is_author( $user_id ) ? ' current-author' : '' ;
    $avatar       = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars['full'] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";

    return apply_filters( 'basic_user_avatar', $avatar );
}

I tried to use lower priority in add_filter('get_avatar') but if i use lower priority plugis not working. Probably because apply_filters in return statement. But i don't know what is problem exactly. How can i fix this problem? Thanks

1
  1. The get_avatar filter accepts 6 parameters, not 5.

  2. The $id_or_email can also be an instance of WP_Post, WP_User or a md5 hash string. So it's better to extract $user_id like this:

 if ( is_numeric( $id_or_email ) ) {
     $user_id = $id_or_email;
 } elseif ( is_string($id_or_email) ) {
     if ( is_email($id_or_email) ) {
         $user = get_user_by('email', $id_or_email);
         $user_id = $user->ID;
     } else { // md5 hash string
         // Do something.
     }
 } elseif ( is_object($id_or_email) ) {
     if ($id_or_email instanceof \WP_User) {
         $user_id = $id_or_email->ID;
     } elseif ($id_or_email instanceof \WP_Post) {
         $user_id = $id_or_email->post_author;
     } else {
         $user_id = $id_or_email->user_id;
     }
 } else {
     return $avatar;
 } 
  1. Make sure the get_user_meta( $user_id, 'basic_user_avatar', true ) return something. If nothing is returned, default $avatar will be used.

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.