New widget: Events for the currentlly displayed group

Dear plugin developer, thanks for the great plugin.
I needed a widget for displaying as widget into the group pages the upcomining events for the currently displayed group.
So I created the following. You can place it at the end of groupcalendar/bp-group-calendar.php file.
After that a widget will be created into the widgets area which you can drop into a sidebar.
The widget will be only displayed at group pages and only if the user has access to the current group.

Can you please give it a try add added it to the next version if you think it is useful for others.

Thanks in advance

Also take a look to the for another issue

         * Shows upcoming events for the currently displayed group
         * @author: lenasterg
class BP_Group_Calendar_Widget_Current_Group extends WP_Widget {

    function BP_Group_Calendar_Widget_Current_Group() {
                $widget_ops = array('classname' => 'bp_group_calendar_single', 'description' => __('Shows upcoming group events for the currentlly displayed group.', 'groupcalendar'));
                $this->WP_Widget('bp_group_calendar_single', __('Current Group Events', 'groupcalendar'), $widget_ops);

            function widget($args, $instance) {
                global $wpdb, $current_user, $bp;

                $group = $bp->groups->current_group;
                $instance['group_id'] = bp_get_current_group_id();
                if ($instance['group_id'] > 0) {
                    // If the group  public, or the user is super_admin or the user is member of group
                    if (($group->status == 'public') || (is_super_admin()) || (groups_is_user_member(bp_loggedin_user_id(), $group_id))) {
                        $group_calendar_url = bp_get_group_permalink($group) . 'calendar/';
                        $date_format = get_option('date_format') . ' ' . get_option('time_format');

                        echo $before_widget;
                        $title = $instance['title'];
                        if (!empty($title)) {
                            echo $before_title . apply_filters('widget_title', $title) . $after_title;
                        $events = $wpdb->get_results("SELECT id, event_title, event_time FROM " . $wpdb->base_prefix . "bp_groups_calendars  WHERE event_time >= '" . date('Y-m-d H:i:s') . "' AND group_id = " . (int) $instance['group_id'] . " ORDER BY event_time ASC LIMIT " . (int) $instance['num_events']);
                        if ($events) {
                            echo '<ul class="events-list">';
                            //loop through events
                            $events_list = '';
                            foreach ($events as $event) {
                                $event_url = $group_calendar_url . 'event/' . $event->id . '/';
                        $events_list .= "<li>";
                                $events_list .= '<a href="' . $event_url . '" title="' . __('View Event', 'groupcalendar') . '">' . date_i18n($date_format, strtotime($event->event_time)) . ': ' . stripslashes($event->event_title) . '</a>';
                                $events_list.= "</li>";
                            echo $events_list;
                            echo "\n</ul>";
                        } else {
                                        <div class="widget-error">
                            <?php _e('There are no upcoming events for this group.', 'groupcalendar') ?>
                        <?php } ?>
                        //Show add event link
                        $calendar_capabilities = bp_group_calendar_get_capabilities();
                        //don't display widget if no capabilities
                        if ($calendar_capabilities != 'none') {
                    echo '<a href="' . $group_calendar_url . '#event-title" title="' . __('Add Event', 'groupcalendar') . '" class="widget-link">' . __('Add event', 'groupcalendar') . '</a>';

                        $url = bp_get_group_permalink($bp->groups->current_group) . 'calendar/';
                        <?php echo $after_widget; ?>

            function update($new_instance, $old_instance) {
                $instance = $old_instance;

                $instance['title'] = strip_tags($new_instance['title']);
                $instance['num_events'] = strip_tags($new_instance['num_events']);
                return $instance;

            function form($instance) {
                global $wpdb;
                $instance = wp_parse_args((array) $instance, array('title' => __('Upcoming Group Events', 'groupcalendar'), 'num_events' => 10));
                $title = strip_tags($instance['title']);
                $num_events = strip_tags($instance['num_events']);
                <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
                <p><label for="<?php echo $this->get_field_id('num_events'); ?>"><?php _e('Number of Events:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('num_events'); ?>" name="<?php echo $this->get_field_name('num_events'); ?>" type="text" value="<?php echo esc_attr($num_events); ?>" style="width: 30%" /></label></p>

                        <p><?php _e('If it is used as a widget into a Buddypress group page will pull the upcoming events for this group, else it won/\'t show anything', 'groupcalendar'); ?></p><?php


add_action('widgets_init', create_function('', 'return register_widget("BP_Group_Calendar_Widget_Current_Group");'));
  • PeterHatch
    • WPMU DEV Initiate

    Hi Lena @stergatu this is a brilliant addition to the calendar, much more useful than the widget with a drop down list of groups, and just exactly what I was looking for. Must admit I was surprised that it worked without tweaking as I am using the latest WP, BP, BuddyBoss and BPGC but it did! Thanks so much!

    Wouldn't it be good if BP caught up with BPGC and enabled rich text and media uploads to update boxes and group descriptions?

  • PeterHatch
    • WPMU DEV Initiate

    I prefer the various events lists to include events that may still be current but have passed their start time so have replaced
    date('Y-m-d H:i:s')
    date('Y-m-d H:i:s', strtotime("-6 hours"))
    on the basis that most events last up to six hours
    the latest version of BPGC used server time but I replaced it with the above and it still works

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.