如何在rest API中公开自定义字段?
我已经向rest api添加了一个自定义字段。问题是,当我在浏览器中签名时,会返回元数据。如果我尝试使用匿名模式作为public,它不会返回自定义字段。我想知道如何将其公开,以便无需登录即可访问。我试着遵循上面提到的内容here
仍然不工作。你能告诉我我错过了什么吗?
我的完整代码:
/**
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;
}