I need an alternative function to use with textareas in meta boxes instead of esc_textarea.

The problem is that when I use this function with textarea's it removes line breaks and paragraphs. Is there an alternative function that will keep the line breaks and paragraphs.

I have tried the validation reference page in the Codex but had no luck finding a function.

4 Answers 4


esc_textarea shouldn't strip out newlines -- It's just a thin wrapper around htmlspecialchars: http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/formatting.php#L2536

function esc_textarea( $text ) {
    $safe_text = htmlspecialchars( $text, ENT_QUOTES );
    return apply_filters( 'esc_textarea', $safe_text, $text );

That said, there are lots of options. What do you want your users to do have the ability to post? esc_html will escape all special characters (just like esc_textarea). esc_attr(strip_tags($stuff)); is a favorite combination of mine.

You should also have a look at the data validation page in the codex.


Sorry for the delay but this was the solution I used. Thanks for the feedback.

$value = esc_html($value);
$value = html_entity_decode($value);

I think you can use wpautop before esc_textarea, like this:

echo esc_textarea( wpautop( $value ) );
  • 2
    autop will insert HTML that esc_textarea will then escape (convert to character entities), so that's probably not going to work. May 28, 2012 at 15:24

Try this:

$lines = "Hello !
From this new line.
echo nl2br($lines);
echo esc_textarea(nl2br($lines));

For me, the output is (but the content-type is text/plain):

Hello !<br />
From this new line.<br />
Hello !&lt;br /&gt;
From this new line.&lt;br /&gt;

I suggest to you to set your content-type with 'text/plain' before.

header('Content-Type: text/plain');

If the content type is by default set to 'text/html', the result will be:

Hello !
From this new line.
Hello !<br /> From this new line.<br />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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