Adding Amchart Interface to WordPress API

Question

I’m using the amchart wordpress plugin and want to add support for it via the wp rest API.

Here’s what I’ve done so far:

in ../includes/setup.php
(To $args array I’ve added: This will create the /wp/v2/charts route)

'show_in_rest'        => true,
'rest_base'           => 'charts'

The custom post type is registered in setup.php as:

  register_post_type( 'amchart', $args );

There are 4 fields in the plugin, and they are all stored in wp_postmeta table

  • _amcharts_resources
  • _amcharts_html
  • _amcharts_javascript
  • _amcharts_slug

Attempting to register the rest fields, I’ve added this to the bottom of the ./inclues/setup.php file:

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);
                },]);
    });

I’ve tried passing both of these payloads to the api via ../wp/v2/charts

{
   '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'
    }
}

In either case, the amchart type post is created, but no associated data is being added to the database. Title is obviously working as it’s standard in post_type = ‘post’, but all meta data is simply not making it into the wp_postmeta table.

Any ideas what I’m doing wrong?

0
, , , stevo22 2 years 2020-01-15T08:41:09-05:00 0 Answers 84 views 0

Leave an answer

Browse
Browse