1

I have added a custom metabox that holds string from a textarea in database. Now if I just simply echo the meta box like this:

$post_meta = get_post_meta($pid);
$answer = $post_meta["answer"][0];
echo $answer;

the html tags would be escaped and the text would appear like this:

blah blah blah <ul><li>blah blah</li><li>blah</li></ul>

As you see the html tags get escaped and they appear as string so I use php like this:

$answer = html_entity_decode($answer);

Now the html tags work as they are expected but there remains one problem. In order to get the line breaks from the database I add another line like this:

    $answer = nl2br($answer);

which solves the problem with line breaks but add a new line break between each li tags.

Any idea how to resolve this? I need line breaks but at the same time would want to avoid adding a line break between li tags.

UPDATE: for the moment I added this line of php after nl2br and I got the result

    $answer = preg_replace("!</li>[\s\S]{1,100}?<!", "</li><", $answer);

But I don't think this is the right way to go.

0

To add line-breaks inside <textarea> (but not on front-end output), use this inside textarea:

echo str_replace('</li>',"</li>\r\n", $answer);
| improve this answer | |
-1

Why don't you use <?php esc_textarea( $answer ); ?>

| improve this answer | |
  • I tested esc_textarea. It escapes every tag inside the string for eample < changes to &lt; – agahi Sep 26 '18 at 8:52
  • Hmm, not sure of the solution top of my mind, but doing research will help. Am sure someone else can come in and help you out. Personally stuck with CPT nonce issue. – Adnan Sep 26 '18 at 9:27
  • 1
    esc_textarea() is for when you are outputting HTML inside a <textarea> element. It has nothing to do with displaying a value saved from a textarea. – Jacob Peattie Sep 26 '18 at 10:45

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.