4

WP frontend output of custom textarea fields not respecting line breakes. In admin it's OK.

Right now I am using this:

<?php $options = get_option('my_custom_plugin_options'); ?>
<?php echo $options['my_custom_plugin_options_textarea']; ?>

The output in frontend is this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris aliquet elementum mi, ac adipiscing metus suscipit in. Quisque venenatis ante non nulla faucibus viverra. In ultrices nisl sit amet quam commodo ut feugiat magna aliquet. Aenean sit amet odio magna. Maecenas pretium ante a urna dictum vel dignissim erat porta. Aenean rhoncus elementum nisi, nec euismod est tempus commodo.

Instead of saved:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Mauris aliquet elementum mi, ac adipiscing metus suscipit in.

Quisque venenatis ante non nulla faucibus viverra.

In ultrices nisl sit amet quam commodo ut feugiat magna aliquet. Aenean sit amet odio magna.

Maecenas pretium ante a urna dictum vel dignissim erat porta. Aenean rhoncus elementum nisi, nec euismod est tempus commodo.

In admin it is saved with as many line breaks and it's OK. How to do it in my frontend too?

5

Explode your $options value by "/n" ( new line ), then do a echo in foreach:

$options = get_option('my_custom_plugin_options');
$textarea = $options['my_custom_plugin_options_textarea'];
$lines = explode("\n", $textarea);

foreach( $lines as $line ){
  echo $line;
}

UPDATE #1

For reference, it's possible to store this function in functions.php to use later in template files.

function the_textarea_value( $textarea ){
        $lines = explode("\n", $textarea);
        foreach( $lines as $line ){
          echo $line;
        }
}

hope it helps!

  • Thanks, it works. I have changed it a little bit by using echo $line . '<br>'; instead of echo '<p>' . $line . '</p>'; in my case. – Derfder Aug 30 '13 at 13:59
  • My bad using <p>, it should do a break line itself – iEmanuele Aug 30 '13 at 14:01
  • Btw. do you know if WP has some function to do it using WP fucntion. E.g. echo original_wp_function_to_do_that($textarea); wthout the need to use explode and foreach? – Derfder Aug 30 '13 at 14:01
  • 1
    @Derfder, here you go: pastebin, try it : ) – iEmanuele Aug 30 '13 at 14:08
9

You can simply use nl2br function from PHP

This should be working for you:

echo nl2br( get_option('my_custom_plugin_options') );

  • 1
    It would be nice if you can provide an example :-) – Pieter Goosen Nov 5 '14 at 4:04
4

I've done another function to do that, and it works in my case:

function the_textarea_value( $textarea ){
    return str_replace("\r\n", '<br>', $textarea); 
}
  • this works like charms, using WP 4+ – Mohammed Sufian Aug 29 '16 at 15:58
0

Elaborating on Pieter Goosen's answer above, using the nl2br function in PHP will achieve the desired outcome.

// Load a customizer setting into a variable
$textarea = get_theme_mod('your_customizer_setting');

// Is there any text in our textarea?
if( !empty($textarea) )
{
    // Once we verify the variable isn't empty, run nl2br
    $textarea = nl2br($textarea);

    // This echos the altered text to the page
    echo $textarea;

}

Example Input:

This is a line of text that I've entered
and this is the line below it

(The line break above would've been created by pressing enter)

Example output:

This is a line of text that I've entered<br>
and this is the line below it

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.