skip to Main Content

hi there m using WooCommerce Memberships by SkyVerge for a membership plan, m trying to make conditional avatars for users and members, when the user is a member their avatar is different and when he is not a member then different, I have two sample codes, can anyone make them conditional

// Custom Default Gravatar Only For Members
add_filter( 'pre_get_avatar', 'custom_gravatar', 1, 3 );
function custom_gravatar( $avatar, $id_or_email, $args ) {
    $uid = get_current_user_id();
    if ( $uid && wc_memberships_get_user_active_memberships( $uid ) ) {
        $avatar = "<img src='https://staranddaisy.in/wp-content/uploads/2022/11/Circle_Design_Membership_Level_GOLD.webp'/>";
    }
    return $avatar;
}

// Default Gravatar For Non-Members or Users
add_filter( 'avatar_defaults', 'wpb_new_gravatar' );
function wpb_new_gravatar( $avatar_defaults ) {
    $myavatar                     = 'http://staranddaisy.in/wp-content/uploads/2022/11/smily_icon_avatar-1.png';
    $avatar_defaults[ $myavatar ] = 'Default Gravatar';
    return $avatar_defaults;
}

2

Answers


  1. Chosen as BEST ANSWER

    function ht1_change_avatar($args, $id_or_email) {
       $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
        if( $active_memberships ){
            $myavatar = 'https://buysnd.com/wp-content/uploads/2022/11/Circle_Design_Membership_Level_GOLD.webp';
        }else{
            $myavatar = 'https://buysnd.com/wp-content/uploads/2022/11/smiley-blushing-emoji-emoticon-3.gif';
        }
        $args['url'] = $myavatar;
        return $args;
    }
    add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);

    function ht1_change_avatar($args, $id_or_email) {
       $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
        if( $active_memberships ){
            $myavatar = 'https://buysnd.com/wp-content/uploads/2022/11/Circle_Design_Membership_Level_GOLD.webp';
        }else{
            $myavatar = 'https://buysnd.com/wp-content/uploads/2022/11/smiley-blushing-emoji-emoticon-3.gif';
        }
        $args['url'] = $myavatar;
        return $args;
    }
    add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
    

  2. I cant test it at full bcs of the membership plugin but this should work

    add_filter( 'pre_get_avatar', 'custom_gravatar', 1, 3 );
    function custom_gravatar( $avatar, $id_or_email, $args ) {
        $uid = get_current_user_id();
        //determine if user is an active member
        if ( $uid && wc_memberships_get_user_active_memberships( $uid ) ) {
            $avatar = "<img src='https://staranddaisy.in/wp-content/uploads/2022/11/Circle_Design_Membership_Level_GOLD.webp'/>";
        } 
        // if not use default 
        else {
            $avatar = "<img src='http://staranddaisy.in/wp-content/uploads/2022/11/smily_icon_avatar-1.png'/>";
        }
        return $avatar;
    }
    
    //Change action if you need
    add_action('init','user_member_avatar');
    function user_member_avatar() {
        if(!is_user_logged_in()) return;
        $uid = get_current_user_id();
        if ( $uid && wc_memberships_get_user_active_memberships( $uid ) ) {
            $avatar_url = esc_url('https://staranddaisy.in/wp-content/uploads/2022/11/Circle_Design_Membership_Level_GOLD.webp');
        } else {
            $avatar_url = esc_url('http://staranddaisy.in/wp-content/uploads/2022/11/smily_icon_avatar-1.png');
        }
        return $avatar_url;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search