bp_activity_set_action( string $component_id, string $type, string $description, callable|bool $format_callback = false, string|bool $label = false, array $context = array(), int $position )

Register an activity ‘type’ and its action description/callback.

Description

Activity actions are strings used to describe items in the activity feed, such as ‘Joe became a registered member’ or ‘Bill and Susie are now friends’. Each activity type (such as ‘new_member’ or ‘friendship_created’) used by a component should be registered using this function.

While it’s possible to post items to the activity feed whose types are not registered using bp_activity_set_action(), it is not recommended; unregistered types will not be displayed properly in the activity admin panel, and dynamic action generation (which is essential for multilingual sites, etc) will not work.

Parameters

$component_id

(Required) The unique string ID of the component.

$type

(Required) The action type.

$description

(Required) The action description.

$format_callback

(Optional) Callback for formatting the action string.

Default value: false

$label

(Optional) String to describe this action in the activity feed filter dropdown.

Default value: false

$context

(Optional) Activity feed contexts where the filter should appear. Values: 'activity', 'member', 'member_groups', 'group'.

Default value: array()

$position

(Optional) The position of the action when listed in dropdowns.

Return

(bool) False if any param is empty, otherwise true.

Source

File: bp-activity/bp-activity-functions.php

function bp_activity_set_action( $component_id, $type, $description, $format_callback = false, $label = false, $context = array(), $position = 0 ) {
	$bp = buddypress();

	// Return false if any of the above values are not set.
	if ( empty( $component_id ) || empty( $type ) || empty( $description ) ) {
		return false;
	}

	// Set activity action.
	if ( ! isset( $bp->activity->actions ) || ! is_object( $bp->activity->actions ) ) {
		$bp->activity->actions = new stdClass;
	}

	// Verify callback.
	if ( ! is_callable( $format_callback ) ) {
		$format_callback = '';
	}

	if ( ! isset( $bp->activity->actions->{$component_id} ) || ! is_object( $bp->activity->actions->{$component_id} ) ) {
		$bp->activity->actions->{$component_id} = new stdClass;
	}

	/**
	 * Filters the action type being set for the current activity item.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param array    $array           Array of arguments for action type being set.
	 * @param string   $component_id    ID of the current component being set.
	 * @param string   $type            Action type being set.
	 * @param string   $description     Action description for action being set.
	 * @param callable $format_callback Callback for formatting the action string.
	 * @param string   $label           String to describe this action in the activity feed filter dropdown.
	 * @param array    $context         Activity feed contexts where the filter should appear. 'activity', 'member',
	 *                                  'member_groups', 'group'.
	 */
	$bp->activity->actions->{$component_id}->{$type} = apply_filters( 'bp_activity_set_action', array(
		'key'             => $type,
		'value'           => $description,
		'format_callback' => $format_callback,
		'label'           => $label,
		'context'         => $context,
		'position'        => $position,
	), $component_id, $type, $description, $format_callback, $label, $context );

	// Sort the actions of the affected component.
	$action_array = (array) $bp->activity->actions->{$component_id};
	$action_array = bp_sort_by_key( $action_array, 'position', 'num' );

	// Restore keys.
	$bp->activity->actions->{$component_id} = new stdClass;
	foreach ( $action_array as $key_ordered ) {
		$bp->activity->actions->{$component_id}->{$key_ordered['key']} = $key_ordered;
	}

	return true;
}

Changelog

Changelog
Version Description
BuddyPress 1.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.