[buddypress-dev] List of users with avatar

Martin martin at thrive-magazine.com
Tue Jul 8 18:57:16 GMT 2008


I'm not sure I can send attachments to the mailing list, so I'll include the
code for the widget I've been working on below. This is my first ever
widget, so if I've done something dumb be gentle :)

This widget shows the latest X (configurable through the widget admin page,
defaults to 5) users, save it to snnewusers.php and upload to the mu-plugins
folder, drop the widget on a widget enabled theme.

Hope this helps,

Martin

<?php
/*
Plugin Name: Social Network New Users
Description: Adds a sidebar widget for showing the newest users
Author: Martin Westwood
Version: 0.1
*/ 

/**********************************************************/
/* Show new users widget control
/**********************************************************/
function widget_sn_showusers() 
{
 	// Check for the required plugin functions. 
	if ( !function_exists('register_sidebar_widget') )
		return;
	
	function widget_show_sn_showusers($args)
	{
	  global $wpdb;
		
	  extract($args);
   
    	// Each widget can store its own options. We keep strings here.
		$options = get_option('widget_snshowusers');
		
		if ($options['user_count'] == '') $options['user_count'] =
5;
		
		$sql = $wpdb->prepare( "SELECT id FROM $wpdb->users ORDER BY
user_registered DESC LIMIT 0, %d", $options['user_count'] );
		$users = $wpdb->get_results( $sql );

		// These lines generate our output. 
		echo $before_widget . $before_title . $options['title'] .
$after_title;
		foreach($users as $user) 
		{
		  $user_info = get_userdata($user->id);
			$blog = get_active_blog_for_user($user->id)
		?>	
		<li class="users">
		  <div class="thumb_user">
		    <a href="<?php echo $blog->siteurl; ?>" title="<?php
echo $user_info->display_name ?>"><?php get_avatar($user->id); ?></a>
		  </div>
		  <div class="text">
		    <a href="<?php echo $blog->siteurl; ?>"><?php echo
$user_info->display_name ?></a>
			</div>
			<div class="clear" />
		</li>	
<?php 
		}
		echo $after_widget;
	}	

	// Admin section
	function widget_control_sn_showusers() {
	 	global $wpdb;
	 	$options = get_option('widget_snshowusers');
	 	if ( !is_array($options) )
			$options = array('title'=>'New
Users','user_count'=>'5');
			
		if ( $_POST['snusers-submit'] ) {

			$options['title'] =
strip_tags(stripslashes($_POST['snusers-title']));
			$options['user_count'] =
strip_tags(stripslashes($_POST['snusers-user_count']));
			update_option('widget_snshowusers', $options);
		}
		
		$title = htmlspecialchars($options['title'], ENT_QUOTES);
		$user_count = htmlspecialchars($options['user_count'],
ENT_QUOTES);
		
		// The Box content
		echo '<p style="text-align:right;"><label
for="snusers-title">' . __('Title:', 'snusers') . ' <input style="width:
200px;" id="snusers-title" name="snusers-title" type="text"
value="'.$title.'" /></label></p>';
		echo '<p style="text-align:right;"><label
for="snusers-user_count">' . __('Number of Users:', 'snusers') . ' <input
style="width: 200px;" id="snusers-user_count" name="snusers-user_count"
type="text" value="'.$user_count.'" /></label></p>';
		echo '<input type="hidden" id="snusers-submit"
name="snusers-submit" value="1" />';
	 		
	}
	
	register_sidebar_widget(array('SN New Users', 'widgets'),
'widget_show_sn_showusers');
	register_widget_control(array('SN New Users', 'widgets'),
'widget_control_sn_showusers', 300, 200);
}

add_action('widgets_init', 'widget_sn_showusers');

?>

-----Original Message-----
From: buddypress-dev-bounces at lists.automattic.com
[mailto:buddypress-dev-bounces at lists.automattic.com] On Behalf Of Andy
Peatling
Sent: Tuesday, July 08, 2008 12:37 PM
To: buddypress-dev at lists.automattic.com
Subject: Re: [buddypress-dev] List of users with avatar 

You'll also need to add:

<?php global $wpdb; ?>

before that code if you put this in page template.

Andy

On 8-Jul-08, at 10:28 AM, Andy Peatling wrote:

> On 8-Jul-08, at 10:05 AM, Brian L. Brey wrote:
>
>> Anyone create code to display a list of members with their avatar  
>> and name (or similar)?
>
> I'm working on the friends component right now, which will  
> essentially do this.
>
> If you want to list members, you can just query the users table, and  
> then get their avatar and link based on their id.
>
> <?php
> $users = $wpdb->get_results ( $wpdb->prepare( "SELECT id FROM  
> wp_users" ) );
> for ( $i = 0; $i < count($users); $i++ ) {
>  $bp_users[] = array(
>    'avatar' => xprofile_get_avatar( $users[$i]->id, 1 ),
>    'link' => bp_core_get_userlink( $users[$i]->id )
>  );
> }
> ?>
>
> Then you can loop through and do whatever you want with $bp_users:
>
> <?php
> for ( $i = 0; $i < count($bp_users); $i++ ) {
>   echo '<p>' . $bp_users[$i]['avatar'] . '</p>';
>   echo '<p>' . $bp_users[$i]['link'] . '</p>';
> }
> ?>
>
> I haven't tested or even tried that code, but it should work if I  
> haven't made any typos.
>
> Andy
>
> ---------
> Andy Peatling | Social Engineer | Automattic
> http://andyinlife.com
>
>
>
> _______________________________________________
> buddypress-dev mailing list
> buddypress-dev at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/buddypress-dev

_______________________________________________
buddypress-dev mailing list
buddypress-dev at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/buddypress-dev



More information about the buddypress-dev mailing list