Skip to content

Instantly share code, notes, and snippets.

@danbp
Created April 9, 2015 01:30
Show Gist options
  • Save danbp/f9c13d0b42c6e42b5545 to your computer and use it in GitHub Desktop.
Save danbp/f9c13d0b42c6e42b5545 to your computer and use it in GitHub Desktop.
BuddyPress - My Groups Widget
<?php
/*
Plugin Name: My Groups Widget
Description: This BuddyPress widget lists the groups of which the logged in user is a member.
Version: 1.0
Revision Date: March 10, 2015
Requires at least: WP 4.0, BuddyPress 2.0
Tested up to: WP 4.1.1, BuddyPress 2.2.1
License: Example: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
Author: Dan
Author URI: http:/bp-fr.net
Site Wide Only: true
*/
/* Register widgets for groups component */
function my_groups_register_widgets() {
add_action('widgets_init', create_function('', 'return register_widget("BP_My_Groups_Widget");') );
}
add_action( 'bp_register_widgets', 'my_groups_register_widgets' );
/*** GROUPS WIDGET *****************/
class BP_My_Groups_Widget extends WP_Widget {
function bp_my_groups_widget() {
parent::WP_Widget( false, $name = __( 'My own Groups', 'buddypress' ) );
}
function widget($args, $instance) {
global $bp;
extract( $args );
$title = apply_filters('widget_title', empty($instance['title'])?__('My own Groups','buddypress'):$instance['title']);
echo $before_widget;
echo $before_title
. $title
. $after_title; ?>
<?php if ( bp_has_groups( 'type=alphabetical&max=3&user_id=' . $bp->loggedin_user->id )&& is_user_logged_in()) : ?>
<ul class="my-groups-list item-list">
<?php while ( bp_groups() ) : bp_the_group(); ?>
<li>
<div class="item-avatar">
<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a>
</div>
<div class="item">
<div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
<div class="item-meta"><span class="activity"><?php bp_group_member_count() ?></span></div>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?>
<input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $instance['title'] ); ?>" />
<?php else: ?>
<div class="widget-error">
<?php if( is_user_logged_in() ) {
_e('You have not joined any groups.','buddypress');
} else {
_e('Please log in to see your groups.', 'buddypress');
} ?>
</div>
<?php endif; ?>
<?php echo $after_widget; ?>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = esc_attr( $instance['title'] );
?>
<p><label><?php _e('Title:','buddypress'); ?></label><input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment