Last active
May 23, 2017 15:16
-
-
Save ChrisLTD/b5cd37653010c2c91b7b28488412bafd to your computer and use it in GitHub Desktop.
Add ACF Fields to WP API responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add ACF Fields to REST responses | |
* Modified from: https://support.advancedcustomfields.com/forums/topic/rest-api/#post-32890 | |
*/ | |
add_action( 'rest_api_init', 'slug_register_acf' ); | |
function slug_register_acf() { | |
$post_types = get_post_types(['public'=>true], 'names'); | |
foreach ($post_types as $type) { | |
register_rest_field( $type, | |
'acf', | |
array( | |
'get_callback' => 'slug_get_acf', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
} | |
function slug_get_acf( $object, $field_name, $request ) { | |
return get_fields($object[ 'id' ]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment