Skip to:
Content
Pages
Categories
Search
Top
Bottom

Register a REST field for the Activity Endpoints


  • scottdev
    Participant

    @scottdev

    Hi I wanted to register a REST field for the Activity Endpoints to get the username of the activity post creator, but I didn’t know how to implement it, I pasted this code in the function.php, but I’m not getting anything back, any help please where this piece of code should be put? Which function.php and how to return the username any help or guidance please

    
    <?php
    
    // Registers a REST field for the Activity Endpoints.
    function example_register_activity_rest_field() {
    	bp_rest_register_field(
    		'activity',      // Id of the BuddyPress component the REST field is about
    		'example_field', // Used into the REST response/request
    		array(
    			'get_callback'    => 'example_get_rest_field_callback',    // The function to use to get the value of the REST Field
    			'update_callback' => 'example_update_rest_field_callback', // The function to use to update the value of the REST Field
    			'schema'          => array(                                // The example_field REST schema.
    				'description' => 'Example of Activity Meta Field',
    				'type'        => 'string',
    				'context'     => array( 'view', 'edit' ),
    			),
    		)
    	);
    }
    add_action( 'bp_rest_api_init', 'example_register_activity_rest_field' );
    
    /**
     * The function to use to get the value of the REST Field.
     *
     * @param  array  $array     The list of properties of the BuddyPress component's object.
     * @param  string $attribute The REST Field key used into the REST response.
     * @return string            The value of the REST Field to include into the REST response.
     */
    function example_get_rest_field_callback( $array, $attribute ) {
    	// The key of the metadata can be different from the REST Field key.
    	$metadata_key = '_example_metadata_key';
    	
    	return bp_activity_get_meta( $array['id'], $metadata_key );
    }
    
    /**
     * The function to use to update the value of the REST Field.
     *
     * @param  object $object    The BuddyPress component's object that was just created/updated during the request.
     *                           (in this case the BP_Activity_Activity object).
     * @return string $value     The value of the REST Field to save.
     * @param  string $attribute The REST Field key used into the REST response.
     */
    function example_update_rest_field_callback( $object, $value, $attribute ) {
    	// The key of the metadata can be different from the REST Field key.
    	$metadata_key = '_example_metadata_key';
    	
    	bp_activity_update_meta( $object->id, $metadata_key, $value );
    }
    
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar