您如何将帖子中定义的自定义字段添加到 wordpress 中的其余 API 响应中 [英] How do you add custom fields defined in posts to the rest API responses in wordpress

查看:27
本文介绍了您如何将帖子中定义的自定义字段添加到 wordpress 中的其余 API 响应中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用任何类型的插件的情况下执行此操作,因为这些都是 wordpress 的核心功能(自定义字段和 REST API).以下是自定义字段的文档以供参考:

以下是当前帖子的 API 响应:

<代码>{_links":{关于": [{"href": "http://example.com/wp-json/wp/v2/types/post"}],作者": [{可嵌入":真的,"href": "http://example.com/wp-json/wp/v2/users/1"}],收藏": [{"href": "http://example.com/wp-json/wp/v2/posts"}],居里":[{"href": "https://api.w.org/{rel}","name": "wp",模板化":true}],回复":[{可嵌入":真的,"href": "http://example.com/wp-json/wp/v2/comments?post=21"}],自己": [{"href": "http://example.com/wp-json/wp/v2/posts/21"}],版本历史":[{"href": "http://example.com/wp-json/wp/v2/posts/21/revisions"}],WP:附件":[{"href": "http://example.com/wp-json/wp/v2/media?parent=21"}],wp:特色媒体":[{可嵌入":真实,"href": "http://example.com/wp-json/wp/v2/media/23"}],"wp:term": [{可嵌入":真实,"href": "http://example.com/wp-json/wp/v2/categories?post=21",分类":类别"},{可嵌入":真的,"href": "http://example.com/wp-json/wp/v2/tags?post=21",分类":post_tag"}]},作者":1,类别":[5、4],"comment_status": "打开",内容": {受保护":假,渲染":"},"日期": "2017-05-14T15:25:33","date_gmt": "2017-05-14T15:25:33",摘录":{受保护":假,渲染":"},特色媒体":23,"格式": "标准",指导":{渲染":http://example.com/?p=21"},身份证":21,"link": "http://example.com/2017/05/14/post/",元":[],"修改": "2017-05-15T18:17:34","modified_gmt": "2017-05-15T18:17:34","ping_status": "打开","slug": "",粘性":假,标签":[],模板": "",标题": {渲染":"},类型":发布"}

如果它可能是相关的,这里是我的活动插件:

解决方案

首先你需要

I want to do this without using any sort of plugin since these are both core wordpress features (custom fields and the REST API). Here is the documentation for custom fields for reference:

https://codex.wordpress.org/Using_Custom_Fields

Here is a screenshot from my wordpress installation:

Here is what the API response for a post looks like currently:

{
    "_links": {
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/1"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts"
            }
        ],
        "curies": [
            {
                "href": "https://api.w.org/{rel}",
                "name": "wp",
                "templated": true
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=21"
            }
        ],
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21"
            }
        ],
        "version-history": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21/revisions"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://example.com/wp-json/wp/v2/media?parent=21"
            }
        ],
        "wp:featuredmedia": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/media/23"
            }
        ],
        "wp:term": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/categories?post=21",
                "taxonomy": "category"
            },
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/tags?post=21",
                "taxonomy": "post_tag"
            }
        ]
    },
    "author": 1,
    "categories": [
        5,
        4
    ],
    "comment_status": "open",
    "content": {
        "protected": false,
        "rendered": ""
    },
    "date": "2017-05-14T15:25:33",
    "date_gmt": "2017-05-14T15:25:33",
    "excerpt": {
        "protected": false,
        "rendered": ""
    },
    "featured_media": 23,
    "format": "standard",
    "guid": {
        "rendered": "http://example.com/?p=21"
    },
    "id": 21,
    "link": "http://example.com/2017/05/14/post/",
    "meta": [],
    "modified": "2017-05-15T18:17:34",
    "modified_gmt": "2017-05-15T18:17:34",
    "ping_status": "open",
    "slug": "",
    "sticky": false,
    "tags": [],
    "template": "",
    "title": {
        "rendered": ""
    },
    "type": "post"
}

In case it could be relevant, here are my active plugins:

解决方案

First you need to register_rest_fields to adding custom endpoints in WP REST API JSON Response

add_action( 'rest_api_init', 'add_custom_fields' );
function add_custom_fields() {
register_rest_field(
'post', 
'custom_fields', //New Field Name in JSON RESPONSEs
array(
    'get_callback'    => 'get_custom_fields', // custom function name 
    'update_callback' => null,
    'schema'          => null,
     )
);
}

Then define your functions to get custom fields

function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}

Tested on local site

这篇关于您如何将帖子中定义的自定义字段添加到 wordpress 中的其余 API 响应中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆