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 Description

Activity actions are strings used to describe items in the activity stream, 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 stream 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 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 stream filter dropdown.

Default value: false

$context

(Optional) Activity stream 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.


Top ↑

Return Return

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


Top ↑

Source 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 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 stream filter dropdown.
	 * @param array    $context         Activity stream 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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.