Last active
April 15, 2024 10:21
-
-
Save vanaf1979/5cdf446be1fdd07ac9d60e1186dc66bd to your computer and use it in GitHub Desktop.
Add custom (ACF) fields to the WordPress Rest Api. https://since1979.dev/add-custom-acf-fields-to-the-wp-rest-api/
This file contains 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 | |
/** | |
* register_featured_image_api_field. | |
* | |
* Register a Rest Api field to add the featured image to the /posts endpoint. | |
* | |
* @see https://since1979.dev/add-custom-acf-fields-to-the-wp-rest-api/ | |
* @uses register_rest_field() https://developer.wordpress.org/reference/functions/register_rest_field/ | |
* @uses array() https://www.php.net/manual/en/function.array.php | |
*/ | |
function register_featured_image_api_field() | |
{ | |
register_rest_field('post', 'featured-image', | |
array( | |
'get_callback' => 'get_featured_image_api_field', | |
'schema' => null, | |
) | |
); | |
} | |
/** | |
* Hook: after_setup_theme. | |
* | |
* @uses add_action() https://developer.wordpress.org/reference/functions/add_action/ | |
* @uses rest_api_init https://developer.wordpress.org/reference/hooks/rest_api_init/ | |
*/ | |
add_action('rest_api_init', 'register_featured_image_api_field'); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment