Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Show only members with Avatars on Member Loop

  • @latinosamorir

    Participant

    Hello.

    So I’m trying to do a member loop that shows only members with avatars.

    Here is my code:

    template member loop file has this line:

    bp_has_members( gd_custom_ids( 'What are your interests?', $interests ) . '&per_page=6&type=random')

    functions.php

    
    /********* SEARCH MEMBERS BASED ON PROFILE FIELDS *********/
    function gd_custom_ids( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
     
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
      else
       return '';
      
      if ( $field_value != '' ) 
        $query .= " AND value LIKE '%" . $field_value . "%'";
          /* 
          LIKE is slow. If you're sure the value has not been serialized, you can do this:
          $query .= " AND value = '" . $field_value . "'";
          */
      
      $custom_ids = $wpdb->get_col( $query );
      $i = 0;
      $user_ids_loop = array();
      foreach ($custom_ids as $custom_id){
    	if(gd_check_avatar($custom_id)){
    	  	$user_ids_loop[$i] = $custom_id;
      		$i++;
      	}
      }  
      
      if ( !empty( $user_ids_loop ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }
    
    /********* CHECK IF MEMBER HAVE AVATARS *********/
    function gd_check_avatar($user_id){
    
    	// Check for avatar.
    		$fbavatar = false;
    		$user_info = get_userdata($user_id);
    	
    		$username=$user_info->user_login;
    	
    		//  Check what information is missing? Avatar? Profile info?
    		$username = strtolower($username);
    		
    		$userfbavatar = get_user_meta($user_ID, 'facebook_uid', true);
    		
    		if (!empty( $userfbavatar ) )
    		{
    			$fbavatar= true;
    			//echo "DEBUG: user has a FB photo<br/>"; 
    		}
    		
    		if (bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true,'html'=> false ) ) != bp_core_avatar_default() )
    		{
    			$has_photo = true; 
    			//echo "DEBUG: user has a photo<br/>"; 
    		}
    		
    		if (  $has_photo || $fbavatar)
    		{
    			$avatarchk = true;
    		}
    		
    		if(!$avatarchk){ $avatarchk=false;}
    		
    		return $avatarchk;
    }

    I have ~6,000 members on my site.

    I get this error:

    [30-May-2014 06:59:51 UTC] PHP Fatal error:  Allowed memory size of 41943040 bytes exhausted (tried to allocate 64 bytes) in /Users/giova/Sites/lam.dev/wp-content/mu-plugins/wpengine-common/patterns.php on line 331
    [30-May-2014 06:59:51 UTC] PHP Fatal error:  Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

    I assume that the for loop in function gd_check_avatar() is not helping.

    What would be the best way to do this to reduce resources and still get only members with avatars?

    Or at least, sort the members so that the first members have an avatar.

    Thanks!

    Giovanni

Viewing 7 replies - 1 through 7 (of 7 total)
  • @rodhewitt

    Participant

    Hello im looking for same solution , im looking for days, all solutions uses too many CPU resources because avatars are not stored in database

    I found this code add to functions.php but its not working for me

    ________________________________________________________________________________________________

    //Hide members without avatars
    add_action( ‘bp_core_delete_existing_avatar’, ‘log_avatar_deleted’ );
    add_action( ‘xprofile_avatar_uploaded’, ‘log_avatar_uploaded’ );

    //on new avatar upload, record it to user meta
    function log_avatar_uploaded(){
    update_user_meta(bp_loggedin_user_id(), ‘has_avatar’, 1);
    }

    //on delete avatar, delete it from user meta
    function log_avatar_deleted($args){
    if($args[‘object’]!=’user’)
    return;
    //we are sure it was user avatar delete
    //remove the log from user meta
    delete_user_meta(bp_loggedin_user_id(), ‘has_avatar’);
    }

    function modify_loop_and_pag($qs, $object=false) {
    global $wpdb;
    $include = ”;

    if( $object != ‘members’ )//hide for members only
    return $qs;

    $subscribers = $wpdb->get_results(“SELECT user_id FROM {$wpdb->usermeta } WHERE meta_key=’has_avatar'” , ARRAY_N );
    foreach($subscribers as $subscriber){
    $include = $include . “,” . $subscriber[0];
    }

    $args = wp_parse_args( $qs );

    //check if we are listing friends?, do not apply in this case

    if( !empty( $args[‘include’] ) ) {
    $args[‘include’] = $args[‘include’] . ‘,’ . $include;
    }
    else {
    $args[‘include’] = $include;
    }

    $qs = build_query($args);

    return $qs;

    }
    add_action( ‘bp_ajax_querystring’ , ‘modify_loop_and_pag’, 25, 2 );

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

    // ATTENTION
    // This code should be deleted after first site load with the code
    //
    function init_avatar_meta(){
    global $wpdb;
    $ids=$wpdb->get_col(“SELECT ID FROM $wpdb->users”);//we don’t need to check for meta value anyway
    foreach( $ids as $id ){
    if( current_user_has_avatar($id) ){
    update_user_meta( $id, ‘has_avatar’, 1 );
    } else {
    delete_user_meta( $id, ‘has_avatar’ );
    }
    }
    }
    add_action(‘init’, ‘init_avatar_meta’);

    @henrywright

    Moderator

    Hi guys

    A way around this would be to hook on to the avatar successfully uploaded action and add a flag to the user’s meta which will tell you if a user has uploaded an avatar. If they delete the avatar then you’d have to remove the flag. This idea would work only for new file uploads so if you have a working site already you’ll need to find a different way.

    @shanebp

    Moderator

    @latinosamorir

    >So I’m trying to do a member loop that shows only members with avatars.

    And only members who have filled out ‘interests’.

    Your code has some mistakes and unnecessary code.
    I don’t know if this will help with the memory issue, but it should be a bit faster.
    https://gist.github.com/shanebp/848d9c4e763fb0a51f2f

    Please use a service like gist to share long code pieces.


    @henrywright

    Yup, that would optimize it a lot.
    But he’s also checking facebook avatars, so the flag needs to account for that.

    @latinosamorir

    Participant

    Thanks @shanebp I’ll take a look!

    Will do use gist in future. =) New to this.

    @henrywright

    Moderator

    But he’s also checking facebook avatars, so the flag needs to account for that.


    @shanebp
    good point. That’s a game changer unless there’s a hook for that too?

    @shanebp

    Moderator

    Not a game changer, just requires (based on his code) a get_user_meta check.

    @henrywright

    Moderator

    I didn’t see facebook_uid is stored in user meta. That does make it easier

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Show only members with Avatars on Member Loop’ is closed to new replies.
Skip to toolbar