V
V
Vitaly Mironov2019-03-14 15:18:26
PHP
Vitaly Mironov, 2019-03-14 15:18:26

How to validate JSON in Wordpress?

Here I am getting json in POST Inside it
$ngp_post_array = $_POST['ngp_rules'];

"name_ru"
"REMOTE_ADDR"
[{"id":"1","name":"\u0420\u043e\u0441\u0441\u0438\u044f","label":"russia","term":"1","country":"\u0420\u043e\u0441\u0441\u0438\u044f","region":"","city":"","data":"\u042d\u0442\u043e \u0440\u0430\u0448\u043a\u0430"},{"id":"2","name":"nsk","label":"nsk","term":"1","country":"","region":"","city":"\u041d\u043e\u0432\u043e\u0441\u0438\u0431\u0438\u0440\u0441\u043a","data":"\u042d\u0442\u043e \u043d\u043e\u0432\u043e\u0441\u0438\u0431\u0438\u0440\u0441\u043a"},{"id":"3","name":"\u0410\u0441\u0442\u0430\u043d\u0430","label":"astana","term":"1","country":"\u0410\u0441\u0442\u0430\u043d\u0430","region":"\u0420\u0435\u0433\u0438\u043e\u043d","city":"\u0413\u043e\u0440\u043e\u0434","data":"\u042d\u0442\u043e astana"}]
[{"id":"1","name":"\u041c\u043e\u0441\u043a\u0432\u0430","city":"\u041c\u043e\u0441\u043a\u0432\u0430","link":"#msk"},{"id":"2","name":"\u041d\u043e\u0432\u043e\u0441\u0438\u0431\u0438\u0440\u0441\u043a","city":"\u041d\u043e\u0432\u043e\u0441\u0438\u0431\u0438\u0440\u0441\u043a","link":"#mskn"}]
"id"
"class"
"ngp_theme_flat_blue"

WP moderators didn't accept my plugin because I don't validate this data in POST.
In fact, I check each cell after json_decode, but apparently this is not enough for them. Here is an example
$name = sanitize_text_field( $data['name'] );
$city = sanitize_text_field( $data['city'] );
$link = sanitize_text_field( $data['link'] );

What can be done in such a situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2019-03-14
@nor1m

$ngp_post_array = array_map( 'sanitize_text_field', wp_unslash( $_POST['ngp_rules'] ) );

Sanitizing array input data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question