I have a basic plugin that pulls tileset data from mapbox in json. I then want to send the json data to my custom javascript to display it. If I pass the data using wp_add_inline_script I can receive the data (proof is by console.loggging the passed data), however, instead of the 12 rows of data, I get 5256 rows of unset data.
Here is my plugin script:
function fs_tilesets_get(){
$url = 'https://api.mapbox.com/tilesets/v1/username?access_token=sk.[snip]';
$arg= [
'headers' => [
'Content-Type' => 'application/json'
],
'body' => []
];
$response = wp_remote_get($url, $args);
//$response_code = wp-wp_remote_retrieve_response_code($response);
$fs_tileset_data = wp_remote_retrieve_body($response);
return $fs_tileset_data;
}
function fs_tilesets_index_html(){
?>
<div class="container">
<div class="box box-primary">
<div class="box-body">
<table width="100%" class="table table-hover" id="fs_tileset_index">
<thead>
<tr>
<th>Type</th>
<th>ID</th>
<th>Name</th>
<th>Center</th>
<th>Created</th>
<th>Modified</th>
<th>Visibility</th>
<th>Description</th>
<th>Filesize</th>
<th>Status</th>
<th>tileset_precisions </th>
<th>created_by_client</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<?php
}
function fs_tileset_settings_admin_callback(){
fs_tilesets_index_html();
}
function fs_api_enqueue_script() {
wp_enqueue_style('datatables-bootstrap5', '//cdn.datatables.net/1.13.3/css/dataTables.bootstrap5.min.css' );
wp_enqueue_style('datatables-responsive', '//cdn.datatables.net/responsive/2.4.0/css/responsive.dataTables.min.css' );
wp_enqueue_script('datatablesjs', '//cdn.datatables.net/1.13.3/js/jquery.dataTables.min.js', array('jquery'), '1.0', TRUE );
wp_enqueue_script('datatablesjsbs', '//cdn.datatables.net/1.13.3/js/dataTables.bootstrap5.min.js', array('jquery'), '1.0', TRUE );
wp_enqueue_script('datatables-responsive', '//cdn.datatables.net/responsive/2.4.0/js/dataTables.responsive.min.js', array('jquery'), '1.0', TRUE );
$fs_plugin_params = array(
'fs_tileset_data' => fs_tilesets_get(),
);
wp_enqueue_script( 'fs_tileset_settings_datatable', plugin_dir_url( __FILE__ ) . 'js/fs_tileset_settings_datatable.js', array('jquery'), '1.0' );
wp_add_inline_script( 'fs_tileset_settings_datatable', 'var fs_plugin_params = ' . wp_json_encode( $fs_plugin_params ), 'before' );
}
add_action('admin_enqueue_scripts', 'fs_api_enqueue_script');
And then in my fs_tileset_settings_datatable.js I have the following;
jQuery( document ).ready(function($) {
console.log(fs_plugin_params.fs_tileset_data);
xxx =
[
{
"type": "vector",
"id": "username.6z20mb78",
"name": "somename-8t9d58",
"center": [
137.18893815,
39.08704805,
0
],
"created": "2023-07-30T02:30:15.315Z",
"modified": "2023-07-30T02:30:15.028Z",
"visibility": "private",
"description": "",
"filesize": 843391,
"status": "available",
"tileset_precisions": {
"10m": 1426540.488293752
},
"created_by_client": "studio"
},
[snip the reset of the records]
];
jQuery('#fs_tileset_index').DataTable({
// data: xxx,
data: fs_plugin_params.fs_tileset_data,
"columns": [
//0
{
data: "type",
"defaultContent": "<i>Not set</i>"
},
//1
{
data: "id",
"defaultContent": "<i>Not set</i>"
},
//2
[snip rest]
If I use xxx it works. If I use fs_plugin_params.fs_tileset_data I get 5236 records instead of 12. The json is passed over as I console log it and it is there (12 of them).
var fs_plugin_params =block look like in the page source? Does that look OK? I wonder if it needs a trailing;.