Created
October 31, 2018 14:14
-
-
Save maximilliangeorge/305894858c6eb53cccac8245523edc44 to your computer and use it in GitHub Desktop.
Add ACF custom fields to the Wordpress JSON API
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 data to the WP JSON REST API | |
add_action('rest_api_init', 'create_api_posts_meta_field'); | |
function create_api_posts_meta_field() { | |
function get_acf($post) { | |
$acf = get_fields($post['ID']); | |
if (isset($post)) { | |
$post['acf'] = $acf; | |
} | |
return $post; | |
} | |
register_rest_field('post', 'acf', array( | |
'get_callback' => 'get_acf' | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment