Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to check if member has avatar ?

  • @megainfo

    Participant

    Hi ,

    I want to check if the current member have an avatar or not in a front page, i try this too method
    1. bp_get_user_has_avatar function return always false

    2 . if ( !bp_core_fetch_avatar( array( ‘item_id’ => $bp->displayed_user->id, ‘no_grav’ => true ) ) ) return always false too.

    Any help thank you ?

Viewing 16 replies - 1 through 16 (of 16 total)
  • @shanebp

    Moderator

    How are you trying to check this?

    Post your code and tell us what page / slug you’re trying to do this on.

    If there is no displayed user and you don’t pass a user_id, it will return false.

    @megainfo

    Participant

    Here is my code

    http://pastebin.com/dCWN34qx

    This my code in functions.php


    function user_has_avatar() {
    global $bp;
    if ( !bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true ) ) )
    return false;
    return true;
    }

    I try to check this in header.php,



    // test one with bp_get_user_has_avatar function
    global $bp;
    if( !bp_get_user_has_avatar()) :
    // echo html message warning
    endif;

    // test two with user_has_avatar function
    if( !user_has_avatar() ) :
    // echo html message warning
    endif;

    bp_core_fetch_avatar( array( ‘item_id’ => $bp->loggedin_user->id, ‘no_grav’ => true, ‘html’=>false )
    return this string :

    http://localhost/wordpress-buddypulse/wp-content/plugins/buddypress/bp-core/images/mystery-man.jpg

    and not false like describe in buddypress docs ( when no_grav is true function will return false if no avatar is added)

    bp_get_user_has_avatar() is used in memberssingleprofilechange-avtar.php , mybe i need to call a funtion to load the data of current member or somthing like that…

    Thanks advance @shanebp for help :)

    @shanebp

    Moderator

    So it will always be true due to the default avatar.

    There might be an existing BP method to ignore both grav and default, but I don’t know what it is.

    So you could string-check, something like this:

    `
    function user_has_avatar() {
    global $bp;

    $avatar = bp_core_fetch_avatar( array( ‘item_id’ => $bp->loggedin_user->id, ‘no_grav’ => true, ‘html’=>false) );

    $pos = strpos($avatar, ‘mystery-man’);
    if ($pos === false) return true;

    return false;
    }
    `

    @megainfo

    Participant

    Thanks @shanebp for answer,

    I wanted a clean solution (just function from buddyrpess) but :(– .
    Any way, here is my function, i used bp_core_avatar_default() to return the default avatar.


    /**
    * Check if the current user has an uploaded avatar
    * @return boolean
    */
    function current_user_has_avatar() {
    global $bp;
    if ( bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true,'html'=> false ) ) != bp_core_avatar_default() )
    return true;
    return false;
    }

    Hope this help some one.
    Closed.

    @studiohyperset

    Participant

    I struggled with this too, but here’s the best solution I came up with:

    :: http://studiohyperset.com/checking-for-avatars-and-gravatars-in-buddypress/4892

    @megainfo

    Participant

    this was work for me


    /**
    * Check if the current user has an uploaded avatar
    * @return boolean
    */
    function current_user_has_avatar() {
    global $bp;
    if ( bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true,'html'=> false ) ) != bp_core_avatar_default() )
    return true;
    return false;
    }

    thanks for answers.

    @nitestop

    Participant

    does anyone know how to add an upload image field to the registration form page…

    @henrywright-1

    Member

    Humm.. this always seems to return true for me @megainfo

    Update: issue resolved. i was defining a custom default avatar hence why the check always trued true e.g

    define ( 'BP_AVATAR_DEFAULT', get_stylesheet_directory_uri() .'/img/avatar.png' );

    @funmi-omoba

    Participant

    @megainfo, @shanebp, please what am I missing.

    I put the below code in my functions.php

    function current_user_has_avatar() {
    
    global $bp;
    
    if ( bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true,'html'=> false ) ) != bp_core_avatar_default() )
    
    return true;
    
    return false;
    
    }

    and this in my header.php

    <?php
    global $bp;
    if( !bp_get_user_has_avatar()) :
    ?>
            <div class="no-pic">
                <strong>Alert !</strong> You have no profile Pic, Clik here to add one now.
                <button class="close" data-dismiss="alert">×</button>
                </div>
    <?php
    endif;

    It work but the alert is not closing when i try to close it

    @henrywright-1

    Member

    If you want to use the function you put in your functions.php then you should use current_user_has_avatar() instead of bp_get_user_has_avatar()

    @henrywright-1

    Member

    So it will look something like this

    <?php if ( current_user_has_avatar() ) {
    
    //do something for people who have uploaded an avatar
    
    } else {
    
    //do something for people who haven't
    
    } ?>

    @funmi-omoba

    Participant

    @henrywright-1, still not working. the check works perfectly but to close the notification is not working. do I need jquery to make the alert close?

    thanks

    @henrywright-1

    Member

    You sure can use jQuery. To hide the alert after the user has clicked on close

    <script>
    $(".close").click(function(){
      $(".no-pic").hide();
    });
    </script>

    @funmi-omoba

    Participant

    @henrywright-1, alert still not closing when user click on close button but thanks for your help anyways.

    @henrywright-1

    Member

    @funmi-omoba

    Make sure you have this inside your <head> </head> in your header.php file

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

    jQuery doesn’t work without it.

    @catyco

    Participant

    The code is working. However, the message is displaying when users log in via social media (Facebook or Twitter). How do I get to ignore login via social media?

Viewing 16 replies - 1 through 16 (of 16 total)
  • The topic ‘How to check if member has avatar ?’ is closed to new replies.
Skip to toolbar