buddypress-functions

BuddyPress is a WordPress plugin that can create a Social Network and Social Community for your BuddyPress WordPress site. Some Designs and Site Structures of WordPress sites with BuddyPress installed are a bit different from normal WordPress.

Here’s a list of custom functions you can add to your theme [highlight color=”” bgcolor=”#eee”]functions.php[/highlight] or [highlight color=”” bgcolor=”#eee”]bp-custom.php[/highlight]

  1. Add Profile meta to BuddyPress Members Directory
  2. Add Twitter, Facebook like and Google Share in BuddyPress Activity Directory
  3. Replace username with user login in BuddyPress Members Directory
  4. Remove @username in single Profile home
  5. Only Allowed Friends to view your Profile page
  6. Remove New Member Registration in Activity Directory
  7. Only Show Friends stream in Activity Directory
  8. Add BuddyPress sitewide activity widget
  9. Add BuddyPress Searchform widget
  10. Add BuddyPress Random Groups widget

[notice type=”info”]Please note: some of the custom functions were collected from other sources which are not originally mine[/notice]

[dropcap color=”#EFC817″ border=””]1[/dropcap]Add Profile Meta to BuddyPress Members Directory

[php]if( !function_exists(‘add_member_custom_extended_profile’) ):
///////////////////////////////////////////////////////////////////////////////
// add profile meta to Buddypress members directory
//////////////////////////////////////////////////////////////////////////////
function add_member_custom_extended_profile() {
$data_jobs = bp_get_member_profile_data( ‘field=Jobs’ );
$data_skills = bp_get_member_profile_data( ‘field=Skills’ );
echo ‘<div class="item-meta"><span class="profile-extend-meta">’;
if($data_jobs) echo ‘<strong>Jobs</strong>’ . ‘: ‘ . $data_jobs . ‘&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;’;
if($data_skills) echo ‘<strong>Skills</strong>’ . ‘: ‘ . $data_skills . ”;
echo ‘</span></div>’;
}
add_action(‘bp_directory_members_item’, ‘add_member_custom_extended_profile’);
endif;[/php]

End Result
After adding the code and edit the preferred profile field, you’ll see something like this in the members directory
buddypress-profile-meta

[dropcap color=”#EFC817″ border=””]2[/dropcap]Add Twitter, Facebook like and Google Share in BuddyPress Activity Directory

[php]if( !function_exists(‘add_social_link_to_activity’) ):
///////////////////////////////////////////////////////////////////////////////
// add social links to BuddyPress activity directory
//////////////////////////////////////////////////////////////////////////////
function add_social_link_to_activity() { ?>
<?php if( bp_is_directory() ): //only show this in activity directory ?>
<div class="social-links activity-<?php bp_activity_id(); ?>">

<a href="http://twitter.com/share" data-url="<?php echo urlencode(bp_activity_thread_permalink()); ?>" data-count="horizontal" data-counturl="<?php echo bp_activity_thread_permalink() ?>" data-text="<?php echo strip_tags( bp_get_activity_action() ); ?>"class="twitter-share-button"><?php _e(‘Tweet’, TEMPLATE_DOMAIN); ?></a>

<g:plus action="share" annotation="bubble" href="<?php echo urlencode(bp_activity_thread_permalink()); ?>"></g:plus>

<iframe src="//www.facebook.com/plugins/like.php?href=<?php echo urlencode( bp_activity_thread_permalink() ); ?>&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;font=arial&amp;colorscheme=light&amp;action=like&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>
</div>
<?php endif; ?>
<?php }
function add_social_link_addthis_js() { ?>
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>
<?php }
add_action(‘bp_activity_entry_content’, ‘add_social_link_to_activity’);
add_action(‘wp_footer’, ‘add_social_link_addthis_js’);
endif;[/php]

End Result
After added the code, browse to activity and you’ll see the social links for twitter, google share and facebook.
twitter-facebook-social-links

[dropcap color=”#EFC817″ border=””]3[/dropcap]Replace username with userlogin in BuddyPress Members Directory

[php]if( !function_exists(‘use_member_userlogin_in_directory’) ):
///////////////////////////////////////////////////////////////////////////////
// Use Member Username in Directory
//////////////////////////////////////////////////////////////////////////////
function use_member_userlogin_in_directory() {
global $members_template;
return $members_template->member->user_login;
}
add_filter(‘bp_member_name’,’use_member_userlogin_in_directory’);
endif;[/php]

[dropcap color=”#EFC817″ border=””]4[/dropcap]Remove @username in single Profile home

[php]if( !function_exists(‘remove_display_username_in_single_profile’) ):
///////////////////////////////////////////////////////////////////////////////
// Remove @username in single Profile home
//////////////////////////////////////////////////////////////////////////////
function remove_display_username_in_single_profile() { ?>
<style> #item-header-content span.user-nicename { display:none; } </style>
<?php }
add_action(‘wp_head’,’remove_display_username_in_single_profile’);
endif;[/php]

[dropcap color=”#EFC817″ border=””]5[/dropcap]Only Allowed Friends to view your Profile page

[php]if( !function_exists(‘only_friends_allowed_in_profile’) ):
///////////////////////////////////////////////////////////////////////////////
// Only allowed Friends to view your profile
//////////////////////////////////////////////////////////////////////////////
function only_friends_allowed_in_profile() {
global $bp;
if ( bp_is_user_activity() || bp_is_user_profile() || bp_is_member() ) {
if ( (‘is_friend’ != BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $bp->displayed_user->id )) && (bp_loggedin_user_id() != bp_displayed_user_id()) ) {
if ( !is_super_admin( bp_loggedin_user_id() ) ) { //exclude super admin
bp_core_redirect( $bp->root_domain ); //redirect to homepage
}
}
}
}
add_filter(‘get_header’,’only_friends_allowed_in_profile’,3);
endif;[/php]

[dropcap color=”#EFC817″ border=””]6[/dropcap]Remove New Member Registration in Activity Directory

[php]if( !function_exists(‘disable_new_member_in_activity’) ):
///////////////////////////////////////////////////////////////////////////////
// Disable new member register in activity
//////////////////////////////////////////////////////////////////////////////
function disable_new_member_in_activity( $a, $activities ) {
// exclude admins
if ( is_site_admin() )
return $activities;
foreach ( $activities->activities as $key => $activity ) {
//new_member is the type name (component is ‘profile’)
if ( $activity->type ==’new_member’) {
unset( $activities->activities[$key] );
$activities->activity_count = $activities->activity_count-1;
$activities->total_activity_count = $activities->total_activity_count-1;
$activities->pag_num = $activities->pag_num -1;
}
}
/* Renumber the array keys to account for missing items */
$activities_new = array_values( $activities->activities );
$activities->activities = $activities_new;
return $activities;
}
add_action(‘bp_has_activities’,’disable_new_member_in_activity’, 10, 2 );
endif;[/php]

End Result
After adding the code, new member registration inactivity will be disabled. source via etiviti

[dropcap color=”#EFC817″ border=””]7[/dropcap]Only Show Friends stream in Activity Directory

[php]if( !function_exists(‘only_show_friend_in_activity’) ):
///////////////////////////////////////////////////////////////////////////////
// Only show friends stream in activity
//////////////////////////////////////////////////////////////////////////////
function my_is_atme_check( $content ) {
global $bp;

if ( !is_user_logged_in() )
return false;

if (!$content)
return false;

$pattern = ‘/[@]+([A-Za-z0-9-_]+)/’;
preg_match_all( $pattern, $content, $usernames );

/* Make sure there’s only one instance of each username */
if ( !$usernames = array_unique( $usernames[1] ) )
return false;

if ( in_array( bp_core_get_username( $bp->loggedin_user->id ), $usernames ) )
return true;

return false;
}

function my_is_friend_check( $friend_id = false) {
global $bp;

if ( !is_user_logged_in() )
return false;

if ( is_site_admin() )
return true;

if ( !$friend_id ) {
$potential_friend_id = $bp->displayed_user->id;
} else {
$potential_friend_id = $friend_id;
}

if ( $bp->loggedin_user->id == $potential_friend_id )
return false;

if ( friends_check_friendship_status($bp->loggedin_user->id, $potential_friend_id) == ‘is_friend’ )
return true;

return false;
}

function my_is_follower_check( $friend_id = false) {
global $bp;

if ( !is_user_logged_in() )
return false;

if ( is_site_admin() )
return true;

if ( !$friend_id ) {
$potential_friend_id = $bp->displayed_user->id;
} else {
$potential_friend_id = $friend_id;
}

if ( $bp->loggedin_user->id == $potential_friend_id )
return false;

if ( bp_follow_is_following( array( ‘leader_id’ => $potential_friend_id, ‘follower_id’ => $bp->loggedin_user->id ) ) )
return true;
}

function only_show_friend_in_activity( $a, $activities ) {
global $bp;
//if admin we want to know
if ( is_site_admin() )
return $activities;
foreach ( $activities->activities as $key => $activity ) {
/* if member of a group – we want the activity even if nonfriend */
if ( $activity->user_id != $bp->loggedin_user->id && $activity->component != ‘groups’ && $activity->component != ‘blogs’ && $activity->user_id != 0 && !my_is_friend_check($activity->user_id) && !my_is_follower_check($activity->user_id) && !my_is_atme_check($activity->content) ) {
unset( $activities->activities[$key] );
$activities->activity_count = $activities->activity_count-1;
$activities->total_activity_count = $activities->total_activity_count-1;
$activities->pag_num = $activities->pag_num -1;
}
}
/* Renumber the array keys to account for missing items */
$activities_new = array_values( $activities->activities );
$activities->activities = $activities_new;
return $activities;
}
add_action( ‘bp_has_activities’, ‘only_show_friend_in_activity’, 10, 2 );
endif;[/php]

End Result
After adding the code, only friends’ activity and updates will be shown in your streams. source via etiviti

[dropcap color=”#EFC817″ border=””]8[/dropcap]Add BuddyPress sitewide activity widget

[php]if( !class_exists(‘My_THEME_BP_Activities_Widget’) ):
///////////////////////////////////////////////////////////////////////////////
// Add BuddyPress activity widget
//////////////////////////////////////////////////////////////////////////////
class My_THEME_BP_Activities_Widget extends WP_Widget {
function My_THEME_BP_Activities_Widget() {
//Constructor
parent::WP_Widget(false, $name = TEMPLATE_DOMAIN . ‘ | Activity’, array(
‘description’ => __(‘Displays your site activities.’, TEMPLATE_DOMAIN)
));
}
function widget($args, $instance) {
// outputs the content of the widget
extract($args); // Make before_widget, etc available.
$ac_name = empty($instance[‘title’]) ? __(‘Site Activity’, TEMPLATE_DOMAIN) : apply_filters(‘widget_title’, $instance[‘title’]);
$ac_count = $instance[‘count’];
$unique_id = $args[‘widget_id’];

echo $before_widget;
echo $before_title . $ac_name . $after_title; ?>
<div class="avatar-block">
<?php if ( bp_has_activities( ‘max=’ . $ac_count . ‘&display_comments=none’ ) ) : ?>
<?php while ( bp_activities() ) : bp_the_activity(); ?>
<?php locate_template( array( ‘activity/entry.php’ ), true, false ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php echo $after_widget;
}

function update($new_instance, $old_instance) {
//update and save the widget
return $new_instance;
}
function form($instance) {
// Get the options into variables, escaping html characters on the way
$ac_name = $instance[‘title’];
$ac_count = $instance[‘count’];
?>
<p>
<label for="<?php echo $this->get_field_id(‘title’); ?>"><?php _e(‘Name’,TEMPLATE_DOMAIN); ?>:</label>
<input id="<?php echo $this->get_field_id(‘title’); ?>" name="<?php echo $this->get_field_name(‘title’); ?>" type="text" class="widefat" value="<?php echo $ac_name; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id(‘count’); ?>"><?php _e(‘How many?’,TEMPLATE_DOMAIN); ?>:</label>
<input id="<?php echo $this->get_field_id(‘count’); ?>" name="<?php echo $this->get_field_name(‘count’); ?>" type="text" class="widefat" value="<?php echo $ac_count; ?>" />
</p>

<?php } }
register_widget(‘My_THEME_BP_Activities_Widget’);

endif;[/php]

End Result
After adding the code, go to widget panel and drag the BuddyPress activty widget and you’ll see this in your homepage with widget.
buddypress-activity

[dropcap color=”#EFC817″ border=””]9[/dropcap]Add BuddyPress Searchform widget

[php]if( !class_exists(‘My_THEME_BP_Searchform_Widget’) ):
///////////////////////////////////////////////////////////////////////////////
// Add BuddyPress Searchform widget
//////////////////////////////////////////////////////////////////////////////

class My_THEME_BP_Searchform_Widget extends WP_Widget {
function My_THEME_BP_Searchform_Widget() {
//Constructor
parent::WP_Widget(false, $name = TEMPLATE_DOMAIN . ‘ | BuddyPress Search’, array(
‘description’ => __(‘Displays your BuddyPress Directory Search.’, TEMPLATE_DOMAIN)
));
}
function widget($args, $instance) {
// outputs the content of the widget
extract($args); // Make before_widget, etc available.
$bps_name = empty($instance[‘title’]) ? __(‘BuddyPress Search’, TEMPLATE_DOMAIN) : apply_filters(‘widget_title’, $instance[‘title’]);
$unique_id = $args[‘widget_id’];
echo $before_widget;
echo $before_title . $bps_name . $after_title; ?>
<?php do_action( ‘bp_before_blog_search_form’ ) ?>
<form action="<?php echo bp_search_form_action() ?>" class="bp-searchform" method="post" id="search-form">
<input type="text" id="search-terms" name="search-terms" onfocus="if (this.value == ‘<?php _e( ‘Start Searching…’, TEMPLATE_DOMAIN ) ?>’) {this.value = ”;}" onblur="if (this.value == ”) {this.value = ‘<?php _e( ‘Start Searching…’, TEMPLATE_DOMAIN ) ?>’;}" />
<?php echo bp_search_form_type_select() ?>
&nbsp;<input type="submit" name="search-submit" id="search-submit" value="<?php _e(‘Search’, TEMPLATE_DOMAIN) ?>" />
<?php wp_nonce_field( ‘bp_search_form’ ) ?>
<?php do_action( ‘bp_blog_search_form’ ) ?>
</form>
<?php do_action( ‘bp_after_blog_search_form’ );
echo $after_widget; ?>
<?php }

function update($new_instance, $old_instance) {
//update and save the widget
return $new_instance;
}
function form($instance) {
// Get the options into variables, escaping html characters on the way
$bps_name = $instance[‘title’];
?>
<p>
<label for="<?php echo $this->get_field_id(‘title’); ?>"><?php _e(‘Name’,TEMPLATE_DOMAIN); ?>:</label>
<input id="<?php echo $this->get_field_id(‘title’); ?>" name="<?php echo $this->get_field_name(‘title’); ?>" type="text" class="widefat" value="<?php echo $bps_name; ?>" /></p>
<?php }
}
register_widget(‘My_THEME_BP_Searchform_Widget’);
endif;[/php]

[dropcap color=”#EFC817″ border=””]10[/dropcap]Add BuddyPress Random Groups widget

[php]if( !class_exists(‘My_THEME_BP_Random_Groups_Widget’) ):
///////////////////////////////////////////////////////////////////////////////
// Add BuddyPress Random Groups widget
//////////////////////////////////////////////////////////////////////////////
function fetch_random_groups($limit=”, $size=”, $type=”, $block_id=”) {
global $wpdb, $bp;
$fetch_group = "SELECT * FROM " . $wpdb->base_prefix . "bp_groups WHERE status = ‘public’ ORDER BY rand() LIMIT $limit";
$sql_fetch_group = $wpdb->get_results($fetch_group); ?>

<ul class="random-groups item-list group-in-<?php echo $block_id; ?>">

<?php
foreach($sql_fetch_group as $group_fe) {
$avatar_full = bp_core_fetch_avatar( ‘item_id=’ . $group_fe->id . ‘&class=avatar&object=group&type=’ . $type . ‘&width=’ . $size . ‘&height=’ . $size );
$group_description = stripslashes($group_fe->description);
?>

<li>
<div class="item-avatar"><?php echo $avatar_full; ?></div>
<div class="item">
<div class="item-title">
<a title="<?php echo $group_fe->name . ‘ – ‘ . get_short_text($group_description, 150); ?>" href="<?php echo home_url() . ‘/’ . bp_get_root_slug( ‘groups’ ) . ‘/’ . $group_fe->slug; ?>"><?php echo $group_fe->name; ?></a>
</div>
<div class="item-meta">
<span class="activity">
<?php echo groups_get_groupmeta( $group_fe->id, $meta_key = ‘total_member_count’); ?> <?php echo bp_get_root_slug( ‘members’ ); ?>
</span>
</div>
</div>
</li>
<?php } ?>
</ul>

<?php }

class My_THEME_BP_Random_Groups_Widget extends WP_Widget {
function My_THEME_BP_Random_Groups_Widget() {
//Constructor
parent::WP_Widget(false, $name = TEMPLATE_DOMAIN . ‘ | Random Groups’, array(
‘description’ => __(‘Displays your BuddyPress Groups Randomly.’, TEMPLATE_DOMAIN)
));
}
function widget($args, $instance) {
// outputs the content of the widget
extract($args); // Make before_widget, etc available.
$rg_name = empty($instance[‘title’]) ? __(‘BuddyPress Random Groups’, TEMPLATE_DOMAIN) : apply_filters(‘widget_title’, $instance[‘title’]);
$unique_id = $args[‘widget_id’];

echo $before_widget;
echo $before_title . $rg_name . $after_title;
echo fetch_random_groups($limit=12, $size=50, $type=’thumb’,$block_id=”);
echo $after_widget;
}

function update($new_instance, $old_instance) {
//update and save the widget
return $new_instance;
}
function form($instance) {
// Get the options into variables, escaping html characters on the way
$rg_name = $instance[‘title’];
?>
<p>
<label for="<?php echo $this->get_field_id(‘title’); ?>"><?php _e(‘Name’,TEMPLATE_DOMAIN); ?>:</label>
<input id="<?php echo $this->get_field_id(‘title’); ?>" name="<?php echo $this->get_field_name(‘title’); ?>" type="text" class="widefat" value="<?php echo $rg_name; ?>" /></p>
<?php
}
}
register_widget(‘My_THEME_BP_Random_Groups_Widget’);

endif;[/php]

End Result
After adding the code, go to the widget panel and drag the BuddyPress random group’s widget and you’ll see this on your homepage with the widget.
buddypress-random-groups

Final Conclusion

If you starting to develop a BuddyPress theme, the above hacks and functions will definitely help you out. You can find more tips and tricks on other resources site: