» » » » 10 Useful Custom Functions For BuddyPress Themes

10 Useful Custom Functions For BuddyPress Themes

buddypress-functions

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

Here’s a list of custom functions you can add to your theme functions.php or bp-custom.php

  1. Add Profile meta to BuddyPress Members Directory
  2. Add Twitter, Facebook like and Google Share in BuddyPress Activty Directory
  3. Replace username with userlogin 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 activty widget
  9. Add BuddyPress Searchform widget
  10. Add BuddyPress Random Groups widget
Please note: some of the custom functions were collected from other sources which are not originally mine
1
Add Profile Meta to BuddyPress Members Directory
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;

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

2
Add Twitter, Facebook like and Google Share in BuddyPress Activty Directory
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;

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

3
Replace username with userlogin in BuddyPress Members Directory
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;
4
Remove @username in single Profile home
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;
5
Only Allowed Friends to view your Profile page
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;
6
Remove New Member Registration in Activity Directory
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;

End Result
After added the code, new member registration in activity will be disable. source via etiviti

7
Only Show Friends stream in Activity Directory
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;

End Result
After added the code, only friends activty and updates will be showed in your streams. source via etiviti

8
Add BuddyPress sitewide activity widget
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;

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

9
Add BuddyPress Searchform widget
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;
10
Add BuddyPress Random Groups widget
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;

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

Final Conclusion

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

10 Useful Custom Functions For BuddyPress Themeshttp://www.magpress.com/wp-content/uploads/2013/02/10-buddypress-functions.jpghttp://www.magpress.com/wp-content/uploads/2013/02/10-buddypress-functions-170x150.jpgRonald
Custom functions for BuddyPress Themes including adding a social links to your activity, privacy for profile, friends activity filter, custom widgets and more..

11 Comments Already

  1. crappy links in facebook share ?p=1234, so likes and shares will be counted 0 even there are some until person share something and refresh page

    Public CommentUsercrappy links in facebook share ?p=1234, so likes and shares will be counted 0 even there are some until person share something and refresh page
  2. Nice buddypress themes. I have create many function using this post. thanks

    Public CommentUserNice buddypress themes. I have create many function using this post. thanks
  3. Amazing! Thank you so much for posting this! 🙂

    Public CommentUserAmazing! Thank you so much for posting this! :)
  4. Hey,
    Thank you!
    But.. I used your function to add bp search widget, I copy your code to my bp_custom.php and got this error:
    Fatal error: Call to a member function register() on a non-object in /home3/dshirac/public_html/allthatsheneeds.com/wp-includes/widgets.php on line 518
    Help please..
    Thanks!

    Public CommentUserHey, Thank you! But.. I used your function to add bp search widget, I copy your code to my bp_custom.php and got this error: Fatal error: Call to a member function register() on a non-object in /home3/dshirac/public_html/allthatsheneeds.com/wp-includes/widgets.php on line 518 Help please.. Thanks!
  5. Thanks for the tutorial on buddypress. This tutorial really helped me.

    Public CommentUserThanks for the tutorial on buddypress. This tutorial really helped me.
  6. BuddyPress themes are nice for community websites.

    Public CommentUserBuddyPress themes are nice for community websites.
  7. Hello to all, the contents existing at this web page are actually
    remarkable for people knowledge, well, keep up the good
    work fellows.

    Public CommentUserHello to all, the contents existing at this web page are actually remarkable for people knowledge, well, keep up the good work fellows.
  8. Good day I am so happy I found your web site,
    I really found you by error, while I was researching on Bing for something else,
    Anyways I am here now and would just like to say thank you for a incredible post
    and a all round thrilling blog (I also love the theme/design), I don’t have time to read it all at the moment but I have saved it and also added in your RSS feeds,
    so when I have time I will be back to read more, Please do keep up the excellent work.

    Public CommentUserGood day I am so happy I found your web site, I really found you by error, while I was researching on Bing for something else, Anyways I am here now and would just like to say thank you for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to read it all at the moment but I have saved it and also added in your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent work.
  9. hello, please am using buddypress 1.8, i want to know how to get a user’s total accumulated favorites from all his activities e.g:
    all activities=30
    all the favorites from all activities= 300.

    i would also like to know how to get a user’s total activities. eg:
    user1=20 activities.
    user2=40 activities.
    thanks.

    Public CommentUserhello, please am using buddypress 1.8, i want to know how to get a user's total accumulated favorites from all his activities e.g: all activities=30 all the favorites from all activities= 300. i would also like to know how to get a user's total activities. eg: user1=20 activities. user2=40 activities. thanks.
    • hi mike, we aren’t familiar with buddypress code.

      Member of MagPressAdministratorhi mike, we aren't familiar with buddypress code.
  10. Awesome tutorial dedicated to BuddyPress. Thanks.

    Public CommentUserAwesome tutorial dedicated to BuddyPress. Thanks.
  11. smart list of funky BP codes. I used the social interaction snippet. Many thanks for that, you are a superstar!

    Public CommentUsersmart list of funky BP codes. I used the social interaction snippet. Many thanks for that, you are a superstar!
  12. Hi,

    Great article! I wanted to implement the share and tweet function. But it doesn’t work. Nothing shows up. I tried it in the functions.php as well in the bp-custom.php

    Is the code working?

    Thanks

    Public CommentUserHi, Great article! I wanted to implement the share and tweet function. But it doesn't work. Nothing shows up. I tried it in the functions.php as well in the bp-custom.php Is the code working? Thanks

Leave a Reply

Please note: Do not SPAM this comment form, all links will be delete immediately