How to check if member has avatar ?
-
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 false2 . if ( !bp_core_fetch_avatar( array( ‘item_id’ => $bp->displayed_user->id, ‘no_grav’ => true ) ) ) return always false too.
Any help thank you ?
-
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.
Here is my code
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
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;
}
`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.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
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.
does anyone know how to add an upload image field to the registration form page…
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' );
@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
If you want to use the function you put in your functions.php then you should use
current_user_has_avatar()
instead ofbp_get_user_has_avatar()
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 } ?>
@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
You sure can use jQuery. To hide the alert after the user has clicked on close
<script> $(".close").click(function(){ $(".no-pic").hide(); }); </script>
@henrywright-1, alert still not closing when user click on close button but thanks for your help anyways.
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.
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?
- The topic ‘How to check if member has avatar ?’ is closed to new replies.