向WordPress API添加AmChart界面

时间:2020-01-15 作者:stevo22

我正在使用amchart wordpress插件,并希望通过wp rest API为其添加支持。

以下是我迄今为止所做的:

在/包括/设置。php(添加到$args数组:这将创建/wp/v2/charts路由)

\'show_in_rest\'        => true,
\'rest_base\'           => \'charts\'
自定义帖子类型已在安装程序中注册。php组件:

  register_post_type( \'amchart\', $args );
插件中有4个字段,它们都存储在wp\\U Posteta表中

试图注册rest字段时,我已将其添加到的底部/包括/设置。php文件:

add_action("rest_api_init", function () {
        register_rest_field("amchart", "_amcharts_html",
            [ "get_callback" => function ($post, $field_name, $request, $object_type) {
                    return get_user_meta($post["id"], $field_name, TRUE);
                },"update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                    update_user_meta($post->ID, $field_name, $value);
                },]);
    });
add_action("rest_api_init", function () {
        register_rest_field("amchart", "_amcharts_javascript",
            [ "get_callback" => function ($post, $field_name, $request, $object_type) {
                    return get_user_meta($post["id"], $field_name, TRUE);
                },"update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                    update_user_meta($post->ID, $field_name, $value);
                },]);
    });
add_action("rest_api_init", function () {
        register_rest_field("amchart", "_amcharts_resources",
            [ "get_callback" => function ($post, $field_name, $request, $object_type) {
                    return get_user_meta($post["id"], $field_name, TRUE);
                },"update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                    update_user_meta($post->ID, $field_name, $value);
                },]);
    });
add_action("rest_api_init", function () {
        register_rest_field("amchart", "_amcharts_slug",
            [ "get_callback" => function ($post, $field_name, $request, $object_type) {
                    return get_user_meta($post["id"], $field_name, TRUE);
                },"update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                    update_user_meta($post->ID, $field_name, $value);
                },]);
    });
我尝试通过..将这两个有效负载传递给api/wp/v2/图表

{
   \'post_type\':\'amchart\',
   \'_amcharts_resources\':\'resource list goes here\',
   \'_amcharts_html\':\'html content\',
   \'_amcharts_javascript\':\'javascript content\',
   \'_amcharts_slug\':\'slug\',
   \'title\': \'title\',
   \'status\': \'publish\'
}

{
    \'post_type\': \'amchart\',
    \'title\': \'title\',
    \'status\': \'publish\',
    \'meta\': {
       \'_amcharts_resources\':\'resource list goes here\',
       \'_amcharts_html\':\'html content\',
       \'_amcharts_javascript\':\'javascript content\',
       \'_amcharts_slug\':\'slug_testing\'
    }
}
在任何一种情况下,都会创建amchart类型的post,但不会向数据库中添加相关数据。显然,Title在post\\u type=\'post\'中是标准的,但所有元数据都没有进入wp\\u postesta表。

知道我做错了什么吗?

1 个回复
最合适的回答,由SO网友:stevo22 整理而成

我在这里找到了一个解决方案。

将此添加到安装程序中的$args。php

\'show_in_rest\'        => true,
\'rest_base\'           => \'charts\'
将其添加到设置的底部。php文件
首先,您要在rest界面中注册要获取/设置的字段。然后将特定元数据注册到post\\u type=amchart

add_action("rest_api_init", function () {
    register_rest_field("amchart", "_amcharts_html",
        [
          "get_callback" => function ($post, $field_name, $request, $object_type) {
                return get_post_meta($post["id"], $field_name, true);
            },
            "update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                return update_post_meta($post->ID, $field_name, $value);
            }
        ]);

    register_rest_field("amchart", "_amcharts_javascript",
      [
        "get_callback" => function ($post, $field_name, $request, $object_type) {
          return get_post_meta($post["id"], $field_name, TRUE);
        },

        "update_callback" => function ($value, $post, $field_name, $request, $object_type) {
          return update_post_meta($post->ID, $field_name, $value);
        }
      ]);

    register_rest_field("amchart", "_amcharts_resources",
        [
          "get_callback" => function ($post, $field_name, $request, $object_type) {
                return get_post_meta($post["id"], $field_name, TRUE);
            },

            "update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                return update_post_meta($post->ID, $field_name, $value);
            }
    ]);

    register_rest_field("amchart", "_amcharts_slug",
        [ "get_callback" => function ($post, $field_name, $request, $object_type) {
                return get_post_meta($post["id"], $field_name, TRUE);
        },

        "update_callback" => function ($value, $post, $field_name, $request, $object_type) {
                return update_post_meta($post->ID, $field_name, $value);
            }
        ]);
});

register_meta(\'amchart\', \'_amcharts_html\', array(
  "type" => "string",
  "show_in_rest" => true,
  "object_subtype" => "amchart"
));
register_meta(\'amchart\', \'_amcharts_javascript\', array(
  "type" => "string",
  "show_in_rest" => true,
  "object_subtype" => "amchart"
));
register_meta(\'amchart\', \'_amcharts_resources\', array(
  "type" => "string",
  "show_in_rest" => true,
  "object_subtype" => "amchart"
));
register_meta(\'amchart\', \'_amcharts_slug\', array(
  "type" => "string",
  "show_in_rest" => true,
  "object_subtype" => "amchart"
));
有效的帖子正文为:

{
   \'post_type\':\'amchart\',
   \'_amcharts_resources\':\'resource list goes here\',
   \'_amcharts_html\':\'html content\',
   \'_amcharts_javascript\':\'javascript content\',
   \'slug\':\'_amcharts_slug\', //or just use the response.id value as the slug if you do not set this explicitly 
   \'title\': \'title\',
   \'status\': \'publish\'  //draft, publish, etc 
}

相关推荐

使用WordPress API从页面获取所有PDF文件

我正在寻找一种使用WordPress API从WordPress帖子中获取所有PDF文件的方法。我获得了所有随附文件:https://www.domain.com/wp-json/wp/v2/media?parent=1267但当我尝试仅获取包含以下内容的PDF文件时,它不起作用:https://www.domain.com/wp-json/wp/v2/media?parent=1267&search=.pdf有什么想法吗?顺致敬意,