BuddyPress Activity Shortcode

Below, you’ll find a detailed guide on how to add the BuddyPress Activity Shortcode Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the BuddyPress Activity Shortcode Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the BuddyPress Activity Shortcode Plugin and the shortcodes it provides:

Plugin Icon
BuddyPress Activity Shortcode

"BuddyPress Activity Shortcode is a reliable WordPress plugin. It enables users to display site-wide activities via shortcode, enhancing the interactive experience on your site."

★★★★★ (7) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [activity-stream]

BuddyPress Activity Shortcode [activity-stream] Shortcode

The bp-activity-shortcode plugin’s ‘activity-stream’ shortcode provides a customizable way to display BuddyPress activity streams on your website. This shortcode allows you to define various parameters including activity title, pagination, load more option, display comments, and more. It also supports filtering and searching, making it highly versatile for different use cases.

Shortcode: [activity-stream]

Parameters

Here is a list of all possible activity-stream shortcode parameters and attributes:

  • title – sets the title of the activity section
  • pagination – controls if pagination is shown or not
  • load_more – controls if the “Load More” option is shown
  • display_comments – determines how comments are displayed
  • include – specifies activity IDs to include in the stream
  • exclude – specifies activity IDs to exclude from the stream
  • in – specifies activity IDs to search among
  • sort – controls the sorting order of activities
  • page – sets the page to load
  • per_page – sets the number of activities per page
  • max – sets the maximum number of activities to return
  • count_total – controls if the total count of activities is shown
  • scope – sets predefined activity filters for a user
  • user_id – filters activities based on user ID
  • object – filters activities based on object like groups, profile
  • action – filters activities based on actions like activity_update
  • primary_id – filters activities based on an object ID
  • secondary_id – filters activities based on a secondary object ID
  • search_terms – specifies terms to search activities on
  • use_compat – determines if the theme compatibility mode is used
  • allow_posting – controls if posting activities is allowed
  • container_class – sets the CSS class of the activity container
  • hide_on_activity – controls if the stream is hidden on activity pages
  • for – sets the context for the activity stream
  • role – filters activities based on user roles
  • for_group – filters activities for a specific group

Examples and Usage

Basic example – Displaying the latest activity with default parameters

[activity-stream /]

Advanced examples

Displaying the latest activity with a custom title, showing pagination, and limiting the number of activities per page to 10.

[activity-stream title="My Custom Title" pagination=1 per_page=10 /]

Filtering the activity stream to only show activities related to a specific user, sorted in ascending order.

[activity-stream user_id=2 sort="ASC" /]

Displaying activities from a specific group, excluding certain activities, and displaying the load more option.

[activity-stream for_group="my-group-slug" exclude="1,2,3" load_more=1 /]

Showing the activity stream with a custom container class, hiding the stream on user and group activity pages, and allowing posting.

[activity-stream container_class="my-custom-class" hide_on_activity=1 allow_posting=1 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [activity-stream] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'activity-stream', array( $this, 'generate_activity_stream' ) );

Shortcode PHP function:

function generate_activity_stream( $atts, $content = null ) {

		// Hide if BuddyPress is not active.
		if ( ! function_exists( 'buddypress' ) ) {
			return '';
		}

		// allow to use all those args awesome!
		$atts = shortcode_atts( array(
			'title'            => 'Latest Activity',// title of the section.
			'pagination'       => 1,// show or not.
			'load_more'        => 0,
			'display_comments' => 'threaded',
			'include'          => false,     // pass an activity_id or string of IDs comma-separated
			'exclude'          => false,     // pass an activity_id or string of IDs comma-separated
			'in'               => false,     // comma-separated list or array of activity IDs among which to search
			'sort'             => 'DESC',    // sort DESC or ASC
			'page'             => 1,         // which page to load
			'per_page'         => 5,         // how many per page.
			'max'              => false,     // max number to return.
			'count_total'      => true,

			// Scope - pre-built activity filters for a user (friends/groups/favorites/mentions).
			'scope'            => false,

			// Filtering
			'user_id'          => false,    // user_id to filter on
			'object'           => false,    // object to filter on e.g. groups, profile, status, friends
			'action'           => false,    // action to filter on e.g. activity_update, new_forum_post, profile_updated
			'primary_id'       => false,    // object ID to filter on e.g. a group_id or forum_id or blog_id etc.
			'secondary_id'     => false,    // secondary object ID to filter on e.g. a post_id.

			// Searching
			'search_terms'     => false,         // specify terms to search on.
			'use_compat'       => bp_use_theme_compat_with_current_theme(),
			'allow_posting'    => false,    // experimental, some of the themes may not support it.
			'container_class'  => 'activity',// default container,
			'hide_on_activity' => 1,// hide on user and group activity pages.
			'for'              => '', // 'logged','displayed','author'.
			'role'             => '', // use one or more role here(e.g administrator,editor etc).
            'for_group'        => '',// group slug.
		), $atts );

		// hide on user activity, activity directory and group activity.
		if ( $atts['hide_on_activity'] &&
		     ( function_exists( 'bp_is_activity_component' ) &&
		       bp_is_activity_component() ||
		       function_exists( 'bp_is_group_home' ) &&
		       bp_is_group_home() )
		) {
			return '';
		}

		$activity_for = $atts['for'];

		if ( ! empty( $activity_for ) ) {
			unset( $atts['for'] );
			$atts['user_id'] =BPAS_Shortcode_Util::get_user_id_for_context( $activity_for );

			if ( empty( $atts['user_id'] ) ) {
				return '';
			}
		}

		$user_ids = array();
		$has_role = false;
		// Fetch users for role and use their activity.
		if ( ! empty( $atts['role'] ) ) {
		    $has_role = true;
			$user_ids        = BPAS_Shortcode_Util::get_user_ids_by_roles( $atts['role'] );
			$atts['user_id'] = $user_ids;
		}

		if ( ! empty( $atts['scope'] ) && 'following' === $atts['scope'] ) {
			// Compatibility for 1.2.2, Not needed when using the 1.3 branch of bp followers.
			$user_id = BPAS_Shortcode_Util::get_user_id_for_context( $activity_for );
			if ( ! $user_id ) {
				$user_id = get_current_user_id();
			}

			$following_ids = array();

			if ( $user_id ) {
				$following_ids = BPAS_Shortcode_Util::get_following_user_ids( $user_id );
			}
			// limit to common users.
			if ( $has_role ) {
				$following_ids = array_intersect( $user_ids, $following_ids );
			}

			// if not following anyone, empty/invalid.
			if ( empty( $following_ids ) ) {
				$atts['user_id'] = array( 0, 0 );// invalid.
			} else {
				$atts['user_id'] = $following_ids;
			}
		}

		$for_group = $atts['for_group'];
		if ( 'groups' === $atts['object'] && ! empty( $for_group ) && bp_is_active( 'groups' ) ) {
			unset( $atts['for_group'] );
			$atts['primary_id'] = BP_Groups_Group::get_id_from_slug( $for_group );

			if ( empty( $atts['primary_id'] ) ) {
				$atts['user_id'] = array( 0, 0 );// no result.
			}
		}

		$this->doing_shortcode = true;
		$is_nouveau = function_exists( 'bp_nouveau' );

		if ( 'groups' === $atts['object'] && ! empty( $atts['primary_id'] ) && is_user_logged_in() ) {
			$atts['show_hidden'] = groups_is_user_member( bp_loggedin_user_id(), $atts['primary_id'] );
		}

		// start buffering.
		ob_start();
		do_action( 'bp_activity_stream_shortcode_before_generate_content', $atts );

		$activity_list_classes = 'activity-list item-list ';

		if ( $is_nouveau ) {
			$activity_list_classes .= ' bp-list';
		}

		$activity_list_classes = apply_filters( 'bpas_activity_list_classes', $activity_list_classes )
		?>

		<?php if ( $atts['use_compat'] ) : ?>
			<div id="buddypress">
		<?php endif; ?>

		<?php if ( $atts['title'] ) : ?>
			<h3 class="activity-shortcode-title"><?php echo $atts['title']; ?></h3>
		<?php endif; ?>

		<?php do_action( 'bp_before_activity_loop' ); ?>

		<?php if ( $atts['allow_posting'] && is_user_logged_in() ) : ?>
        <div class="bpas-post-form-wrapper">
	        <?php bp_locate_template( array( 'activity/post-form.php' ), true ); ?>

        </div>
		<?php endif; ?>

		<?php if ( bp_has_activities( $atts ) ) : ?>

            <div class="bpas-shortcode-activities <?php echo esc_attr( $atts['container_class'] ); ?> <?php if ( ! $atts['display_comments'] ) : ?> hide-activity-comments<?php endif; ?> shortcode-activity-stream">

                <ul id="activity-stream" class="<?php echo esc_attr( $activity_list_classes ); ?> ">

					<?php while ( bp_activities() ) : bp_the_activity(); ?>
						<?php bp_get_template_part( 'activity/entry' ); ?>
					<?php endwhile; ?>

					<?php if ( $atts['load_more'] && bp_activity_has_more_items() ) : ?>
                        <li class="load-more">
                            <a href="<?php bp_activity_load_more_link() ?>"><?php _e( 'Load More', 'buddypress' ); ?></a>
                        </li>
					<?php endif; ?>
                </ul>

				<?php if ( $atts['pagination'] && ! $atts['load_more'] ) : ?>
                    <div class="pagination">
                        <div class="pag-count"><?php bp_activity_pagination_count(); ?></div>
                        <div class="pagination-links"><?php bp_activity_pagination_links(); ?></div>
                    </div>
				<?php endif; ?>

            </div>

        <?php else : ?>
            <div id="message" class="info">
                <p><?php _e( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ); ?></p>
            </div>
        <?php endif; ?>

		<?php do_action( 'bp_after_activity_loop' ); ?>
        <form name="bpas-activities-args" class="bpas-activities-args">
            <input type="hidden"  class="bpas_input_display_comments" name="display_comments" value="<?php echo esc_attr( $atts['display_comments'] ) ?>">
            <input type="hidden" class="bpas_input_include" name="include" value="<?php echo esc_attr( $atts['include'] ) ?>">
            <input type="hidden" class="bpas_input_exclude" name="exclude" value="<?php echo esc_attr( $atts['exclude'] ) ?>">
            <input type="hidden" class="bpas_input_int" name="in" value="<?php echo esc_attr( $atts['in'] ) ?>">
            <input type="hidden" class="bpas_input_sort" name="sort" value="<?php echo esc_attr( $atts['sort'] ) ?>">
            <input type="hidden" class="bpas_input_page bps-input-current-page" name="page" value="<?php echo esc_attr( $atts['page'] + 1 ) ?>">
            <input type="hidden" class="bpas_input_per_page" name="per_page" value="<?php echo esc_attr( $atts['per_page'] ) ?>">
            <input type="hidden" class="bpas_input_max" name="max" value="<?php echo esc_attr( $atts['max'] ) ?>">
            <input type="hidden" class="bpas_input_count_total" name="count_total" value="<?php echo esc_attr( $atts['count_total'] ) ?>">
            <input type="hidden" class="bpas_input_scope" name="scope" value="<?php echo esc_attr( $atts['scope'] ) ?>">

            <input type="hidden" class="bpas_input_user_id" name="user_id" value="<?php echo esc_attr( $atts['user_id'] ) ?>">
            <input type="hidden" class="bpas_input_object" name="object" value="<?php echo esc_attr( $atts['object'] ) ?>">
            <input type="hidden" class="bpas_input_bpas_action" name="bpas_action" value="<?php echo esc_attr( $atts['action'] ) ?>">
            <input type="hidden" class="bpas_input_primary_id" name="primary_id" value="<?php echo esc_attr( $atts['primary_id'] ) ?>">
            <input type="hidden" class="bpas_input_secondary_id" name="secondary_id" value="<?php echo esc_attr( $atts['secondary_id'] ) ?>">
            <input type="hidden" class="bpas_input_search_terms" name="search_terms" value="<?php echo esc_attr( $atts['search_terms'] ) ?>">
            <input type="hidden" class="bpas_input_for" name="for" value="<?php echo esc_attr( $activity_for ) ?>">
            <input type="hidden" class="bpas_input_role" name="role" value="<?php echo esc_attr( $atts['role'] ) ?>">
            <input type="hidden" class="bpas_input_for_group" name="for_group" value="<?php echo esc_attr( $for_group ); ?>">
            <input type="hidden" class="bpas_input_wpnonce" name="_wpnonce" value="<?php echo wp_create_nonce( 'bpas_load_activities' ) ?>">
            <!--<input type="hidden" name="action" value="bpas_load_activities">-->
        </form>

		<form action="" name="activity-loop-form" id="activity-loop-form" method="post">
			<?php wp_nonce_field( 'activity_filter', '_wpnonce_activity_filter' ); ?>
		</form>

		<?php if ( $atts['use_compat'] ) : ?>
			</div>
		<?php endif; ?>

		<?php

		$output = ob_get_clean();

		$this->doing_shortcode = false;
		do_action( 'bp_activity_stream_shortcode_after_generate_content', $atts );

		return $output;
	}

Code file location:

bp-activity-shortcode/bp-activity-shortcode/core/class-bpas-shortcode-helper.php

Conclusion

Now that you’ve learned how to embed the BuddyPress Activity Shortcode Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *