Skip to content

Instantly share code, notes, and snippets.

@dryan1144
Created December 24, 2019 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dryan1144/8a97f2b6a97cd3861f02187818633fe4 to your computer and use it in GitHub Desktop.
Save dryan1144/8a97f2b6a97cd3861f02187818633fe4 to your computer and use it in GitHub Desktop.
Define basic custom field and callback function
<?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