Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm collecting data via a form plugin and save 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 via

<?php 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 text/paragraph field 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?

Any help would be highly appreciated!

Many thanks in advance, Thomas

share|improve this question
add comment

4 Answers

I 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.

share|improve this answer
add comment

This seems to work fine:

<? $source = get_post_meta($post->ID, '_aboutus', true);
if($source){
$source = explode("\n", $source); ?>
<? for($i = 0;$i<sizeof($source);$i++){ ?>
<? echo $source[$i]; ?><br />
<? } ?>
<? } ?>
<?php if(get_post_meta($post->ID, '_aboutus', true)):                                       echo '</div>'; endif; ?>
share|improve this answer
add comment

This could probably help,

http://codex.wordpress.org/Function_Reference/wpautop

This converts the new lines into html <br/> tags

share|improve this answer
add comment

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.

/* 
 * Recreate the default filters on the_content
 * this will make it much easier to output the meta content with proper/expected formatting
*/
add_filter( 'meta_content', 'wptexturize'        );
add_filter( 'meta_content', 'convert_smilies'    );
add_filter( 'meta_content', 'convert_chars'      );
add_filter( 'meta_content', 'wpautop'            );
add_filter( 'meta_content', 'shortcode_unautop'  );
add_filter( 'meta_content', 'prepend_attachment' );

Then the template code would look like:

<?php 
if( $about = get_post_meta($post->ID, '_aboutus', true ) ): 
  echo '<div class="companyaboutus">'. apply_filters( 'meta_content', $about ) . '</div>'; 
endif; 
?>

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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