WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post form that sends data to a page which uses wp_insert_post to create a post. I can easily sanitize most data, but I have some troubles with my tags, I retrieve in an array:

$tags =  $_POST['tags'];

Before using these tags as tags_input, how can I successfully sanitize all names?


share|improve this question

Here's a way to do it with PHP's array map function:

// Good idea to make sure things are set before using them
$tags = isset( $_POST['tags'] ) ? (array) $_POST['tags'] : array();

// Any of the WordPress data validation functions can be used here
$tags = array_map( 'esc_attr', $tags );
share|improve this answer
+1 Looks like the right way to do it — stackoverflow.com/a/4861080/535406 – jibiel May 15 '13 at 9:30
Yep, This looks like the way to go. – Miguel Garrido Dec 3 '15 at 23:56
up vote 2 down vote accepted

If anyone is interested, I solved it like this:

        $tags = $_POST['tags'];
        if (count($tags) > 5){
            echo 'Niet meer dan 5 tags';
            $stop = true;

        if (is_array($tags)) {
            foreach ($tags as &$tag) {
                $tag = esc_attr($tag);
            unset($tag );
        } else {
            $tags = esc_attr($tags);
share|improve this answer
you should use sanitize_term instead of esc_attr, for your particular case – onetrickpony Aug 21 '11 at 13:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.