Created
December 24, 2019 18:08
-
-
Save dryan1144/8a97f2b6a97cd3861f02187818633fe4 to your computer and use it in GitHub Desktop.
Define basic custom field and callback function
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 custom field for existing endpoint with callback function | |
Endpoint: /wp/v2/your_post_type | |
Note: Could also be a custom endpoint using register_rest_route | |
***/ | |
register_rest_field('your_post_type', 'custom_field_name', array( | |
'get_callback' => 'yournamespace_endpoint_callback', | |
'schema' => null, | |
)); | |
/*** | |
Callback function - if you use a custom endpoint you may have to change how you get $post_id | |
***/ | |
function yournamespace_endpoint_callback( $object ) { | |
$post_id = $object['id']; | |
return yournamespace_get_post_transient( $post_id, 'yournamespace_data_example', 'yournamespace_get_custom_meta' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment