Created
March 26, 2022 08:56
-
-
Save wpflames/e84affc33601955a094d10ae2a5ade2b to your computer and use it in GitHub Desktop.
Add Custom Fields to Rest 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 Custom Fields to Rest API | |
| // ============================================================= | |
| function add_rest_api_custom_fields() { | |
| register_rest_field('post', 'authorName', array( | |
| 'get_callback' => function() {return get_the_author();} | |
| )); | |
| register_rest_field('post', 'yourCustomField', array( | |
| 'get_callback' => function() {return get_field('custom-field-name');} | |
| )); | |
| } | |
| add_action('rest_api_init', 'add_rest_api_custom_fields'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment