I wanted to sanitize a WP_Query post object without using foreach.
I used this method:
$args = array(
// ...
)
$data = array();
$the_query = new WP_Query($args);
$data["post"] = $the_query->posts;
array_map("sanitize_post", $data["post"]);
return rest_ensure_response($data);
output:
as you can see that filter is working and its turning to "Display" from "Raw".
But the problem is: <script>
tags still standing in post_title field.
Whats the problem?
I wanted to sanitize a WP_Query post object without using foreach.
I used this method:
$args = array(
// ...
)
$data = array();
$the_query = new WP_Query($args);
$data["post"] = $the_query->posts;
array_map("sanitize_post", $data["post"]);
return rest_ensure_response($data);
output:
as you can see that filter is working and its turning to "Display" from "Raw".
But the problem is: <script>
tags still standing in post_title field.
Whats the problem?
Share Improve this question asked Feb 21, 2021 at 14:44 techn9netechn9ne 213 bronze badges 18 | Show 13 more comments1 Answer
Reset to default 0Solution:
sanitize_post is coming with "display" default filter option. And it does not sanitize tags.
But if you tries a code like this:
$sanitizer = function($post){
return sanitize_post($post, "db"); // or "edit"
};
array_map($sanitizer, $data["post"]);
Now it sanitizes fields against XSS!
sanitize_post
is supposed to remove script tags. – Jacob Peattie Commented Feb 21, 2021 at 14:47show_in_rest
adds, and usepre_get_posts
to make any adjustments to what it displays to the user. – Tom J Nowell ♦ Commented Feb 21, 2021 at 15:53