Skip to content

Instantly share code, notes, and snippets.

@nathansh
Created August 2, 2015 22:44
Show Gist options
  • Save nathansh/733d606d36bb438c724f to your computer and use it in GitHub Desktop.
Save nathansh/733d606d36bb438c724f to your computer and use it in GitHub Desktop.
Add ACF fields to WP JSON API V2
<?php
function d7_add_acf_to_json_api_v2($object, $field_name, $request){
if ( function_exists('get_fields') ) {
return get_fields($object['id']);
}
}
if ( is_plugin_active('rest-api/plugin.php') ) {
add_filter('rest_api_init', function(){
// Get all post types
$types = get_post_types(array('_builtin' => false), 'objects');
// Find post types that are in the api
foreach ( $types as $slug => $type ) {
if ( isset($type->show_in_rest) && $type->show_in_rest ) {
// Register the type's ACF fields
register_api_field($slug, 'custom_fields', array(
'get_callback' => 'd7_add_acf_to_json_api_v2',
'update_callback' => null,
'schema' => null
));
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment