Skip to content

Instantly share code, notes, and snippets.

@thsneumann
Last active March 9, 2017 12:17
Show Gist options
  • Save thsneumann/cff5711dffccbb6c7b62be2594d4bb3d to your computer and use it in GitHub Desktop.
Save thsneumann/cff5711dffccbb6c7b62be2594d4bb3d to your computer and use it in GitHub Desktop.
<?php
/*** Add Advanced Custom Fields to REST API GET Response ***/
/*** tested in WP4.7.3 with ACF4.4.11
/*** https://support.advancedcustomfields.com/forums/topic/json-rest-api-and-acf/ ***/
add_filter('rest_prepare_post', 'rest_api_encode_acf', 10, 3);
function rest_api_encode_acf($response, $post, $request) {
if (!function_exists('get_fields')) return $response;
if (isset($post)) {
$acf = get_fields($post->id);
$response->data['acf'] = $acf;
}
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment