Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active March 25, 2019 12:58
Show Gist options
  • Save BretCameron/09518857156e42ab8e51b5a7ed25475b to your computer and use it in GitHub Desktop.
Save BretCameron/09518857156e42ab8e51b5a7ed25475b to your computer and use it in GitHub Desktop.
Register a custom field type as a REST field in WordPress
<?php // Register a Custom Field Type as a REST field
function register_genre_as_rest_field() {
register_rest_field(
'movies',
'genre',
array(
'get_callback' => 'get_genre_meta_field',
'update_callback' => null,
'schema' => null,
)
);
};
function get_genre_meta_field( $object, $field_name, $value ) {
return get_post_meta($object['id'])[$field_name][0];
};
add_action( 'rest_api_init', 'register_genre_as_rest_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment