bp_activity_set_action

Register an activity 'type' and its action description/callback.

Description

bp_activity_set_action( (string) $component_id, (string) $type, (string) $description, (constant) $format_callback = false, (constant) $label = false, (array) $context = array(), (int) $position = 0 ); 

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 (7)

0. $component_id (string)
The unique string ID of the component.
1. $type (string)
The action type.
2. $description (string)
The action description.
3. $format_callback — Optional. (constant) => false
Callback for formatting the action string.
4. $label — Optional. (constant) => false
String to describe this action in the activity stream filter dropdown.
5. $context — Optional. (array) => array()
Activity stream contexts where the filter should appear. Values: activity,, member., member_groups, group.
6. $position — Optional. (int)
The position of the action when listed in dropdowns.

Usage

  1. if ( !function_exists( 'bp_activity_set_action' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The unique string ID of the component. 
  5. $component_id = ''; 
  6.  
  7. // The action type. 
  8. $type = ''; 
  9.  
  10. // The action description. 
  11. $description = ''; 
  12.  
  13. // Callback for formatting the action string. 
  14. $format_callback = false; 
  15.  
  16. // String to describe this action in the activity stream filter dropdown. 
  17. $label = false; 
  18.  
  19. // Optional. Activity stream contexts where the filter should appear. 
  20. // Values: 'activity', 'member', 'member_groups', 'group'. 
  21. $context = array(); 
  22.  
  23. // Optional. The position of the action when listed in dropdowns. 
  24. $position = -1; 
  25.  
  26. // NOTICE! Understand what this does before running. 
  27. $result = bp_activity_set_action($component_id, $type, $description, $format_callback, $label, $context, $position); 
  28.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_set_action( $component_id, $type, $description, $format_callback = false, $label = false, $context = array(), $position = 0 ) { 
  2. $bp =buddypress); 
  3.  
  4. // Return false if any of the above values are not set. 
  5. if ( empty( $component_id ) || empty( $type ) || empty( $description ) ) { 
  6. return false; 
  7.  
  8. // Set activity action. 
  9. if ( ! isset( $bp->activity->actions ) || ! is_object( $bp->activity->actions ) ) { 
  10. $bp->activity->actions = new stdClass; 
  11.  
  12. // Verify callback. 
  13. if ( ! is_callable( $format_callback ) ) { 
  14. $format_callback = ''; 
  15.  
  16. if ( ! isset( $bp->activity->actions->{$component_id} ) || ! is_object( $bp->activity->actions->{$component_id} ) ) { 
  17. $bp->activity->actions->{$component_id} = new stdClass; 
  18.  
  19. /** 
  20. * Filters the action type being set for the current activity item. 
  21. * @since 1.1.0 
  22. * @param array $array Array of arguments for action type being set. 
  23. * @param string $component_id ID of the current component being set. 
  24. * @param string $type Action type being set. 
  25. * @param string $description Action description for action being set. 
  26. * @param callable $format_callback Callback for formatting the action string. 
  27. * @param string $label String to describe this action in the activity stream filter dropdown. 
  28. * @param array $context Activity stream contexts where the filter should appear. 'activity', 'member',  
  29. * 'member_groups', 'group'. 
  30. */ 
  31. $bp->activity->actions->{$component_id}->{$type} = apply_filters( 'bp_activity_set_action', array( 
  32. 'key' => $type,  
  33. 'value' => $description,  
  34. 'format_callback' => $format_callback,  
  35. 'label' => $label,  
  36. 'context' => $context,  
  37. 'position' => $position,  
  38. ), $component_id, $type, $description, $format_callback, $label, $context ); 
  39.  
  40. // Sort the actions of the affected component. 
  41. $action_array = (array) $bp->activity->actions->{$component_id}; 
  42. $action_array = bp_sort_by_key( $action_array, 'position', 'num' ); 
  43.  
  44. // Restore keys. 
  45. $bp->activity->actions->{$component_id} = new stdClass; 
  46. foreach ( $action_array as $key_ordered ) { 
  47. $bp->activity->actions->{$component_id}->{$key_ordered['key']} = $key_ordered; 
  48.  
  49. return true;