Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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
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.