Skip to:
Content
Pages
Categories
Search
Top
Bottom

Avatar does not show outside BuddyPress


  • paixaofileto
    Participant

    @paixaofileto

    I used this code in function.php:

    wpse_49216_my_new_avatar_url function () {
         $gender = xprofile_get_field_data ('ranking', bp_get_member_user_id ());
    if ($gender == "Top 25") {
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/25.png';
    }
    if ($gender == "Top 24") {
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/24.png';
    }
    }
    add_filter ('bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url');
    
    function wpse_49216_filter_bp_avatar ($html) {
         return preg_replace ('/src=".+?"/', 'src =' 'wpse_49216_my_new_avatar_url ()..' "', $ html);
    }
    add_filter ('bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar');
    

    The avatar will not appear outside buddypress page, on any other page it appears. I read on the internet that I must pass the id of the User with bp_loggedin_user_id (). How do I implement this in my code?

Viewing 17 replies - 1 through 17 (of 17 total)

  • danbp
    Participant

    @danbp

    Try this:

    function wpse_49216_my_new_avatar_url() {
    global $bp;
    	if( bp_is_user() && !bp_get_member_user_id() ) {
            $user_id = 'displayed: '. bp_displayed_user_id();
        } else {
            $user_id = 'get_member_user: '. bp_get_member_user_id();
        }
    
    $gender = xprofile_get_field_data ( 'ranking', $user_id );
       if ($gender == "Top 25") {
          return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/25.png';
       }
       if ($gender == "Top 24") {
          return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/24.png';
       }
    }
    add_filter ('bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url');

    paixaofileto
    Participant

    @paixaofileto

    gave the following error:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘: 1)’ at line 1]
    SELECT id, user_id, field_id, value, last_updated FROM wp_bp_xprofile_data WHERE field_id = 2 AND user_id IN (displayed: 1)

    Read on the internet that I must pass the id as parameter for bp_core_fetch_avatar:

    <?php
    $userid = bp_loggedin_user_id();
    echo bp_core_fetch_avatar( array( ‘item_id’ => $userid) );
    ?>

    But do not know where I have to put this code or how to implement in my code. Can you help me?


    danbp
    Participant

    @danbp

    Oh wait… what do you exactly want to achieve ? Where should this ranking avatar show up ?
    Give more details please.

    What is the profile field name: ranking or gender ?
    Theme name ?


    paixaofileto
    Participant

    @paixaofileto

    Here is the link to my site:
    http://hsseek2.esy.es/

    you can log in and see:

    username: test
    password: test

    Avatars not appear on pages outside of BuddyPress, as the home page for example. In BuddyPress pages appear in the web page does not appear.

    The profile name is ranking, used the gender variable because adapted from another code and not changed.

    My theme is Twnty Fifteen


    danbp
    Participant

    @danbp

    Your homepage contains an error:
    ob_end_flush(): failed to send buffer of zlib output compression (1) in ../../wp-includes/functions.php on line 3269

    Which kind of avatar should appear on the homepage ? It’s the default blog page and there is no avatar on this page.

    You have to setup buddypress correctly at first before trying to modify your site.

    Check that any BP component has his page and setup pretty permalinks.

    Configure BuddyPress


    paixaofileto
    Participant

    @paixaofileto

    already removed the error. you can see that in the area of members appears the avatar and the homepage will not appear?

    Area of members:
    Appear

    Homepage:
    not appear


    paixaofileto
    Participant

    @paixaofileto

    The avatars only works when I’m in BuddyPress pages.
    I want to make it work around the site by adding the following code passing the user ID I found searching for:

    <? php
    $ Userid = bp_loggedin_user_id ();
    echo bp_core_fetch_avatar (array ('item_id' => $ id user));
    ?>

    But in which .phpeu add file this code?


    danbp
    Participant

    @danbp

    Can you publish the exact code you’re actually using to show custom avatars on your site ?

    Your first snippet contains errors.

    You have to refresh the page once logged-in to get the avatar to appear in the sidebar.


    paixaofileto
    Participant

    @paixaofileto

    the code I am using is:

    wpse_49216_my_new_avatar_url function () {
    
    global $ bp;
    
         $ranking = xprofile_get_field_data ('ranking', bp_get_member_user_id ());
    if ($ranking == "Top 25") {
    
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/25.png';
    }
    if ($ gender == "Top 24") {
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/24.png';
    }
    }
    add_filter ('bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url');
    
    function wpse_49216_filter_bp_avatar ($ html) {
         return preg_replace ('/src=".+?"/', 'src =' 'wpse_49216_my_new_avatar_url ()..' "', $ html);
    }
    
    add_filter ('bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar');

    in function.php


    danbp
    Participant

    @danbp

    Sorry but your pasted code is wrong.
    See source.
    Read also the comments before you copy/paste.


    @henrywright
    can you help out please ?


    paixaofileto
    Participant

    @paixaofileto

    Thanks for trying to help me @danbp.

    So I want to save an avatar for a user depending on what he select the field xprofile. It is possible? How do I?


    paixaofileto
    Participant

    @paixaofileto

    Tried a new code using bp_profile_field_data:

    wpse_49216_my_new_avatar_url function () {
    
    $ current_user = wp_get_current_user ();
    $ current_user_id = $ current_user-> ID;
    
    global $ bp;
    
         $ var = bp_profile_field_data (array ('user_id' => $ current_user_id, 'field' => 'ranking'));
    if ($ var == "Cranking") {
    
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/lendario.png';
    }
    if ($ var == "Dranking") {
    return 'http://hsseek2.esy.es/wp-content/uploads/2015/04/25.png';
    }
    }
    add_filter ('bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url');
    
    function wpse_49216_filter_bp_avatar ($ html) {
         return preg_replace ('/src=".+?"/', 'src =' 'wpse_49216_my_new_avatar_url ()..' "', $ html);
    }
    
    add_filter ('bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar');

    It now appears the name of the field on the homepage, but I need to treat the value returned by bp_profile_field_data it looks like is:

    http://hsseek2.esy.es/

    username: teste
    password: 1234

    What value the bp_profile_field_data returns? How can I treat it?


    danbp
    Participant

    @danbp

    Previous snippet changed all avatars (user, blogs and groups) which is not what you want. Here’s a snippet which works almost correctly, but not to 100%. Give it a try anyway.

    It changes the user avatar depending of an xprofile value.

    (add to bp-custom.php)

    function bpfr_my_new_avatar_url() {
    global $bp;
    
    	if( bp_is_user() && ! bp_get_member_user_id() ) {
            $user_id = bp_displayed_user_id();
        } else {
            $user_id = bp_get_member_user_id();
        }
    
    	if ( is_page( 'activity' ) ) {
    		$user_id = bp_get_activity_user_id();	
    	}
    
    $ranking = xprofile_get_field_data ('ranking', $user_id );
    
    	if ($ranking == "Top 25") {		
    		$url = get_stylesheet_directory_uri() .'/custom_img/25.png';
    	}
    	
    	if ($ranking == "Top 24") {		
    		$url = get_stylesheet_directory_uri() .'/custom_img/24.png';
    	}
    return $url;
    
    }
    add_filter( 'bp_core_default_avatar_user', 'bpfr_my_new_avatar_url' );
    

    Create a profile field called “ranking” with 2 options (top 24 & top 25).
    Create a folder in your child-theme called “custom_img” and add 2 pictures: 24.png & 25.png (250×250)
    If everybody must those avatars, disallow avatar upload in BP settings.
    Site default avatar stays as Mystery Man (wp default setting).

    You have also to get rid of Gravatar, so you must add this to bp-custom.php
    add_filter('bp_core_fetch_avatar_no_grav', '__return_true');

    The thing who doesn’t work is related to X & Y are now friends. Both willhave the same avatar.

    Can probably by better improved.


    paixaofileto
    Participant

    @paixaofileto

    Thank you. But still did not solve my problem. He appears in the members and activity page, but the web page still does not appear, as the home page and admin manager. Has no solution for this?

    I’m trying to edit the plugin BuddyPress Fistr Letter Avatar because it changes the avatar everywhere the site taking the value of the first letter of the name of the user. But always when I try to use a value of xprofile field it does not work the way I want.

    Has no solution for this? Any suggestion?


    danbp
    Participant

    @danbp

    Finally, i got it to work by improving my previous code.

    This one works correctly now.

    function bpfr_my_new_avatar_url( $local_url, $params ) {
    
    // ensure we have always the right user_id
    $user_id = $params['item_id'];
    
    $ranking = xprofile_get_field_data ('ranking', $user_id );
    
    	if ($ranking == "Top 25") {		
    		$local_url = get_stylesheet_directory_uri() .'/custom_img/25.png';
    	}
    	
    	if ($ranking == "Top 24") {		
    		$local_url = get_stylesheet_directory_uri() .'/custom_img/24.png';
    	}
    return $local_url;
    
    }
    add_filter( 'bp_core_default_avatar_user', 'bpfr_my_new_avatar_url', 10, 2 );

    paixaofileto
    Participant

    @paixaofileto

    Thanks @danbp, I thought I had already forgotten me. But this code did not work here. There is appearing on every page avatars? And when someone enters the profile that appears avatar?


    danbp
    Participant

    @danbp

    What is not working ?
    I use it on a test site without any problems. All user who have selected a rank on their profile, have the belonging avatar. And this custom avatar is showing everywhere (groups, activity, friends, group forum and site wide forum.

    That said, I don’t use any avatar plugins.

Viewing 17 replies - 1 through 17 (of 17 total)
  • The topic ‘Avatar does not show outside BuddyPress’ is closed to new replies.
Skip to toolbar