Skip to content

Instantly share code, notes, and snippets.

@darrenjaworski
Last active February 15, 2016 17:46
Show Gist options
  • Save darrenjaworski/408ccc64066c2672cfb5 to your computer and use it in GitHub Desktop.
Save darrenjaworski/408ccc64066c2672cfb5 to your computer and use it in GitHub Desktop.
use custom field suite with wp-api
<?php
add_action( 'rest_api_init', 'example_add_cfs' );
/**
* Add cfs fields to api call for post type example
* Fields will be put in cfs object in json
* @param none
*
* @return null
*/
function example_add_cfs() {
register_rest_field( 'example',
'cfs',
array(
'get_callback' => 'add_cfs_to_json',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function add_cfs_to_json( $object, $field_name, $request ) {
return CFS()->get( false, $object[ 'id' ], array( 'format' => 'raw' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment