1

Disclaimer: I have read and searched, quite a lot.

I am building my data to send via AJAX like this:

var plugin_data = [
    plugin_action_button.attr('data-action-to-take'),
    plugin_action_button.attr('data-plugin-slug')
];
requestPluginAction( JSON.stringify(plugin_data) );

If I am to console.log, then it translates to:

["activate","handle"]

If I am to return what PHP is seeing, here's what it is:

[\"activate\",\"handle\"]

Now, running a simple json_decode on that string:

check_ajax_referer( 'plugin_routines', 'security' );
$data = sanitize_text_field( $_POST['plugin_install_request_data'] );
wp_send_json( json_decode( $data ) );

Returns null.

I've tried a lot of things. json_decode with true set on, simply returning the unfiltered value I got from AJAX, etc.

But none works.

I'm ultimately looking to convert that JSON to a PHP array.

coolpasta
  • 725
  • 5
  • 19
  • 1
    [*Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE **and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.***](http://php.net/manual/en/function.json-decode.php) – Script47 Jun 08 '18 at 00:31
  • @Script47 Makes sense, yet I still don't see the solution. – coolpasta Jun 08 '18 at 00:34
  • 2
    I don't think you want [`sanitize_text_field()`](https://developer.wordpress.org/reference/functions/sanitize_text_field/) for JSON data. Does your PHP instance happen to have magic-quotes enabled? – Phil Jun 08 '18 at 00:35
  • 1
    Without seeing all your code, it might possibly be a sanitize function that could be causing it, but again, that is just a guess. – Script47 Jun 08 '18 at 00:35
  • I'm pretty sure Phil is right. Don't sanitize the data before sending it or processing it – Machavity Jun 08 '18 at 00:39
  • 1
    @Phil Disgusting. That did it. Yup, that was right. Thanks so much. Don't code at 6 AM. – coolpasta Jun 08 '18 at 00:40
  • @coolpasta wow, your PHP version must be really old. `magic_quotes_gpc` hasn't been a valid config since PHP 5.3 – Phil Jun 08 '18 at 00:41

0 Answers0