admin管理员组文章数量:1386872
How do i make the custom fields public in rest API?
I have added a custom field to the rest api. the challenge is - The metadata is returned when i am signed in my browser. if i try as public using incognito mode - it doesn't return the custom field. I was wondering how do i make it public so that i can access without loggin in. i tried following what is mentioned here
Still not working. Can you let me know what am i missing?
My Full code :
/**
Changes Related to adding Additional Field "thumbnail_image" in Post
*/
function addCustomField_register_fields() {
register_rest_field('post',
'thumbnail_image',
array(
'get_callback' =>'get_custom_field',
'update_callback' => null,
'schema' => null
));
}
function get_custom_field($post, $field_name , $request) {
return get_post_meta($post['id'], 'thumbnail_image', true);
}
add_action('rest_api_init', 'addCustomField_register_fields');
//* Make the field public */
add_filter( 'rest_api_allowed_public_metadata', 'allow_thumbnail_metadata' );
function allow_thumbnail_metadata() {
// only run for REST API requests
if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
return $allowed_meta_keys;
$allowed_meta_keys[] = 'thumbnail_image';
return $allowed_meta_keys;
}
How do i make the custom fields public in rest API?
I have added a custom field to the rest api. the challenge is - The metadata is returned when i am signed in my browser. if i try as public using incognito mode - it doesn't return the custom field. I was wondering how do i make it public so that i can access without loggin in. i tried following what is mentioned here
Still not working. Can you let me know what am i missing?
My Full code :
/**
Changes Related to adding Additional Field "thumbnail_image" in Post
*/
function addCustomField_register_fields() {
register_rest_field('post',
'thumbnail_image',
array(
'get_callback' =>'get_custom_field',
'update_callback' => null,
'schema' => null
));
}
function get_custom_field($post, $field_name , $request) {
return get_post_meta($post['id'], 'thumbnail_image', true);
}
add_action('rest_api_init', 'addCustomField_register_fields');
//* Make the field public */
add_filter( 'rest_api_allowed_public_metadata', 'allow_thumbnail_metadata' );
function allow_thumbnail_metadata() {
// only run for REST API requests
if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
return $allowed_meta_keys;
$allowed_meta_keys[] = 'thumbnail_image';
return $allowed_meta_keys;
}
Share
Improve this question
edited Apr 13, 2017 at 12:37
CommunityBot
1
asked Mar 15, 2017 at 15:45
Night MongerNight Monger
1012 bronze badges
1 Answer
Reset to default -1Check out this post under the "Get Posts Meta Field Is Not Available By Default" heading: https://1fix.io/blog/2015/07/20/query-vars-wp-api/
本文标签: post metaMake Custom Fields Public in JSONAPI
版权声明:本文标题:post meta - Make Custom Fields Public in JSON - API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744490816a2608721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论