1. Developer Tutorials
  2. Extending the BuddyBoss App API

Extending the BuddyBoss App API

In this tutorial, you will learn how to add your custom API data with existing WordPress and BuddyBoss Platform endpoint responses. You may want to reference the BuddyBoss App API and the BuddyBoss Platform API, which are both used in the app.

Registering a Rest Field

The register_rest_field function is the most flexible way to add fields to the REST API response objects. This function accepts three parameters:


1. Object_type

The name of the object, as a string, or an array of the names of objects for which the field is being registered. This may be a core WordPress type like “post”, “terms”, “meta”, “user” or “comment”, but can also be the string name of a custom post type.

2. Attribute

The name of the field. This name will be used to define the key in the response object.

3. Args

An array with keys that define the callback functions used to retrieve the value of the field (‘get_callback’), to update the value of the field (‘update_callback’), and to define its schema (‘schema’).


Each of the keys of the $args array is optional, but if not used, that capability will not be added. This means that you may specify a callback function for reading the value and omit the update callback to make that field read-only if desired.

Fields should be registered at the rest_api_init action. 

Reference: https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

Example

register_rest_field('bp_activity',
			'buddyboss_media_ids',
			array(
				'get_callback' => array($this, "get_media_meta"),
				'update_callback' => array($this, "update_media_meta"),
				'schema' => null,
			)
		);

Updating an Endpoint Response

For existing endpoints, we have provided filters in several places before the response dispatches. By using the correct filter you can also update the endpoint response.

As an example, for BuddyBoss Platform endpoints, you can find all available filters here: https://www.buddyboss.com/resources/?s=bp_rest&subset=cpt-wp-parser-hook&bp_search=1

Note: BuddyBoss Platform filters must have bp_rest as a prefix followed by the component name. LearnDash API must have bbapp_ld_ as a prefix followed by the object name.

Example

/**
* Restrict Forum for Mobile App to support PmProMembership
*
* @param \WP_REST_Response $response Endpoint Response
* @param object $post forum Post
* @param \WP_REST_Request $request Endpoint Request
*
* @return mixed
*/
public function forum_restrict_pmpro_rule( $response, $post, $request ) {

	if ( isset( $response->data['current_user_permissions'] ) ) {

		$hasAccess = pmpro_has_membership_access( $post->ID );
		if ( ! $hasAccess ) {
				$response->data['current_user_permissions']['show_subforum'] = false;
				$response->data['current_user_permissions']['show_topics']   = false;
				$response->data['current_user_permissions']['topic']         = false;
		}
	}

	return $response;
}
add_filter('rest_prepare_forum', 'forum_restrict_pmpro_rule', 999, 3);

Purging Cache for an existing endpoint

For BuddyBoss App Core endpoints, we support API caching. You can view our API Caching tutorial to learn how the feature works, to improve the performance of API responses in the app.

We provide two filters for each endpoint to add custom events for purging:

1. An item’s cache only needs to be purged if your event changes the order of items or if you are adding/removing items from the list. 

BuddyBoss\Performance\Cache::instance()->purge_by_group( $integration_name );

2. The individual item cache of the item’s endpoint will be cleared with a single endpoint cache event. For removing a single endpoint cache event, you need to use the below method:

BuddyBoss\Performance\Cache::instance()->purge_by_group( $integration_name . '_' . $item_id   );

Here is the $integration_name for all endpoints:

Pages and Posts

App Page	: app_page
Post		: blog_post
Comment		: post_comment
Categories	: categories

BuddyBoss Platform

Activity 	: bp-activity
Group 		: bp-groups
Member 		: bp-members
Message 	: bp-messages
Notification	: bp-notifications
Connection 	: bp-friends
Forum 		: bbp-forums
Discussion 	: bbp-topics
Reply 		: bbp-replies
Document	: bp-document
Album		: bp-media-albums
Photo		: bp-media-photos

LearnDash

Course		: sfwd-courses
Course Details	: sfwd-courses-details
Lesson		: sfwd-lessons
Topic		: sfwd-topic
Quiz		: sfwd-quiz

Questions?

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