Last active
March 25, 2019 12:58
-
-
Save BretCameron/09518857156e42ab8e51b5a7ed25475b to your computer and use it in GitHub Desktop.
Register a custom field type as a REST field in WordPress
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 // 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