Is it possible to create Amcharts via wp API
-
Hi,
I’m trying to see if there is a way to interact with the amcharts plugin via the wp api and can not find anything in that regard through my searches.
Anyone have any insight on this?
Cheers,
Steven
-
Since WP API allows creating any post type, you can create charts as well.
However, you would need to reverse-engineer either plugin code or existing DB records, in order to know how to populate those posts.
I’m afraid we do not offer any specialized amCharts-specific API.
Thanks for the reply.
I’m looking through it now, and the only thing that I’m struggling with (so far) is the amcharts_nonce. Is that required? I’m not sure how I’ll be able to replicate that from an api perspective.
It looks like this may do the trick, but it’ll fail without a validated nonce.
{ 'post_type':'amchart', 'amcharts_nonce':nonce, 'resources':'resource list goes here', 'html':'html content', 'javascript':'javascript content', 'slug':'slug-id', }nonce is used to prevent resubmission of forms. It should not be relevant if you are creating WP posts directly.
in setup.php
add
‘show_in_rest’ => true,to the args
update args:
'supports' => array( 'title' , 'html' , 'javascript' , 'slug' , 'resources' ),You can override the route by specifying rest_base in args by the way.
I’m getting a 201 response with the payload above(minus nonce). but none of the values are populated in the draft chart/post.
Any thoughts on what I can do from here?
I’d love to write a full post on how to do this from start to finish.I’ve tried _amcharts_resources type of nomenclature as well (this is the database postmeta naming convention) but nothing I’ve tried has actually populated the meta fields in the chart.
OK, i got this to work. 🙂
add the 2 items above in the top of setup.py
and at the bottom of setup.py add:
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" ));a working example is:
payload body:
{ 'post_type': 'amchart', // 'title': 'title', 'status': 'publish', '_amcharts_resources':'resource list goes here', '_amcharts_html':'html content', '_amcharts_javascript':'javascript content', '_amcharts_slug':'slug_testing' }
The topic ‘Is it possible to create Amcharts via wp API’ is closed to new replies.