0

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?

1 Answer 1

0

I was able to find a solution here.

Add this to $args in setup.php

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

Add this to the bottom of the setup.php file.
fistly, you're registering the fields to get/set in the rest interface. then you're registering the specific meta data to the post_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"
));

The valid post body is:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.