Skip to content

Instantly share code, notes, and snippets.

@imath

imath/bp-custom-1.php

Last active Nov 14, 2020
Embed
What would you like to do?
Examples to show BP REST API 7.0.0 improvements.
<?php
/**
* Showing the `friendship_status_slug` Members Endpoint's schema property.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/members\',
type: \'GET\',
data: {
context: \'view\',
type: \'active\',
exclude: [2]
}
} ).done( function( data ) {
data.forEach( function( member ) {
console.log( member.name + \' => \' + member.friendship_status_slug );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the `populate_extras` Members/:user_id request argument.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/members/2\',
type: \'GET\',
data: {
context: \'view\',
populate_extras: true
}
} ).done( function( member ) {
console.log( \'Last activity:\' );
console.log( member.last_activity );
console.log( \'Latest activity update:\' );
console.log( member.latest_update );
console.log( \'Total number of friends:\' );
console.log( member.total_friend_count );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the `member_type` request argument for the members/:user_id endpoint.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/members/2\',
type: \'PUT\',
data: {
context: \'edit\',
member_type: \'leads,developers\'
}
} ).done( function( member ) {
console.log( \'Member Types\' );
console.log( member.member_types );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the groups/me endpoint.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/groups/me\',
type: \'GET\',
data: {
context: \'view\'
}
} ).done( function( groups ) {
console.log( \'My Groups\' );
groups.forEach( function( group ) {
console.log( group.name );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the `populate_extras` Groups/:group_id request argument.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/groups/1\',
type: \'GET\',
data: {
context: \'view\',
populate_extras: true
}
} ).done( function( groups ) {
groups.forEach( function( group ) {
console.log( \'Last activity date:\' );
console.log( group.last_activity );
console.log( \'Last activity time diff:\' );
console.log( group.last_activity_diff );
console.log( \'Total number of group members:\' );
console.log( group.total_member_count );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the groups/:group_id update method new request arguments.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/groups/1\',
type: \'PUT\',
data: {
context: \'view\',
append_types: \'team,pizzalovers\'
}
} ).done( function( groups ) {
console.log( \'The initial + appended group types:\' );
groups.forEach( function( group ) {
console.log( group.types );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the activity streams filtered on 2 actions/types.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/activity\',
type: \'GET\',
data: {
context: \'view\',
type: [\'activity_update\', \'friendship_created\', \'friendship_accepted\']
}
} ).done( function( activities ) {
console.log( \'Updates and created friendships:\' );
activities.forEach( function( activity ) {
console.log( activity.id + \' => \' + activity.type );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
<?php
/**
* Showing the activity streams filtered on a specific group.
*/
function test_bp_rest_api() {
wp_enqueue_script( 'bp-api-request' );
wp_add_inline_script(
'bp-api-request',
'bp.apiRequest( {
path: \'buddypress/v1/activity\',
type: \'GET\',
data: {
context: \'view\',
group_id: 1
}
} ).done( function( activities ) {
console.log( \'Group Activities:\' );
activities.forEach( function( activity ) {
console.log( activity.id + \' => \' + activity.component );
} );
} ).fail( function( error ) {
return error;
} );'
);
}
add_action( 'bp_enqueue_scripts', 'test_bp_rest_api' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.