Skip to content

Instantly share code, notes, and snippets.

@imath
Created March 4, 2013 16:03
Show Gist options
  • Save imath/5083281 to your computer and use it in GitHub Desktop.
Save imath/5083281 to your computer and use it in GitHub Desktop.
A BuddyPress trick in reply to http://buddypress.org/support/topic/display-only-members-who-have-uploaded-avatar/ : the goal is to only loop through the members who actually uploaded an avatar (instead of using gravatar). !important Make sure to backup your db before running init_evo_meta(). Once init_evo_meta() ran, make sure to delete it and l…
<?php
/************* beginning of the code to paste in the functions.php of the active theme *************/
/*
the function to to display only the users that actually uploaded an avatar
see http://buddypress.org/support/topic/display-only-members-who-have-uploaded-avatar/
*/
function evo_list_uploaded_avatars(){
// just add a meta_key to the member loop args
$args = array( 'type' => 'random', 'max' => 6, 'meta_key' => 'evo_user_uploaded_avatar', 'meta_value' => 1 );
if ( bp_has_members( $args ) ) : ?>
<ul>
<?php while ( bp_members() ) : bp_the_member();?>
<li><a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar('type=full&width=125&height=125');?></a></li>
<?php endwhile;?>
</ul>
<?php endif;
}
/* if a user uploads an avatar we store a meta */
function evo_user_uploaded_avatar(){
$user_id = bp_displayed_user_id();
if( !empty( $user_id ) )
update_user_meta( $user_id, 'evo_user_uploaded_avatar', 1 );
}
add_action( 'xprofile_avatar_uploaded', 'evo_user_uploaded_avatar');
/* if a user deletes an avatar we delete the meta */
function evo_user_deleted_avatar( $args ) {
$user_id = bp_displayed_user_id();
if( !empty( $user_id ) )
delete_user_meta( $user_id, 'evo_user_uploaded_avatar' );
}
add_action( 'bp_core_delete_existing_avatar', 'evo_user_deleted_avatar', 10, 1 );
/** beginning of the code to delete once ran **/
function init_evo_meta(){
$members = bp_core_get_users( array( 'per_page' => false, 'page' => false ) );
foreach( $members['users'] as $member ){
$has_avatar = bp_core_fetch_avatar( array( 'object' => 'user', 'item_id' => $member->ID, 'no_grav' => 1, 'html' => false ) );
if( !empty( $has_avatar ) )
update_user_meta( $member->ID, 'evo_user_uploaded_avatar', 1 );
}
}
/* First make a db backup! To make init_evo_meta() works, you need to uncomment the 2 following lines */
//add_action( 'admin_menu', 'init_evo_meta');
//add_filter( 'bp_core_default_avatar_user', create_function( '', 'return false;' ) );
/*Then display just once your WordPress Backend and delete the function init_evo_meta and the 2 previous lines */
/** end of the code to delete once ran **/
/************* end of the code to paste *************/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment