I’m collecting data via a form plugin and saving that data in form of a post with several custom fields. I then display the content of the post and the custom fields on my post template using…
if( get_post_meta( $post->ID, '_aboutus', true ) ) :
echo '<div class="companyaboutus">' . get_post_meta($post->ID, '_aboutus', true) . '</div>';
endif;
The form field for the “About Us” text is a textarea and most contributors add several lines of text into the field. Displaying the content/text via the code above doesn’t show any line breaks though – any idea how to preserve the line breaks?
I stuck with the same question and actually tested
wpautop()
. It worked, e.g.Still, I came around one issue. When sanitizing the user input, donât use a sanitization method that removed the line breaks before using wpautop 🙂
This could probably help,
http://codex.wordpress.org/Function_Reference/wpautop
This converts the new lines into html
<br/>
tagsI dont’t know which form plugin you use and how it saves data to the database. But I would asume that the linebreaks are saved to the database.
But as you might know, a line break in the source code of a HTML page does not show a line break on the browser. The is a very handy PHP function called nl2br() which will add a
tag for each line break in the user content text.
What I like to do is re-create all the default filters that are applied to
the_content
by adding the following in functions.php.Then the template code would look like:
You could, of course, use
the_content
but I found that plugins also like to hook into that for displaying sharing buttons and the like and so this method avoids sharing buttons from taking over your page.This seems to work fine:
Concerts
n
to<br>
Use
wp_kses
with an empty array as theallowed_html
parameter. This will sanitize the string without removing line breaks and should pass most coding standards