THE PENDING DRAFT

Sanitize Text Field with Multiple Lines in WordPress

June 26, 2015

I recently ran into a problem where we had a text field in a metabox, that was meant to be able to hold multiple paragraphs of text, but still be properly sanitized.

The text sanitization (sanitize_text_field) removes all tags, including line-breaks, so i had to find a workaround. This is the best solution i found:

$pdr_multiline_text = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['pdr_text_field'] ) ) );

Basically, what it does is breaking up the given string into smaller chunks on line-breaks (Implode on "\n"), then sanitize those smaller strings using sanizite_text_field() and then put them back together (explode on "\n").

Stackoverflow – How to sanitize multi-line text from a textarea without losing line breaks?

Leave your comment