Preserving line breaks when saving and displaying custom fields data

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?

7 s
7

I stuck with the same question and actually tested wpautop(). It worked, e.g.

echo wpautop(get_post_meta($post->ID, '_aboutus', true));

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 🙂

Leave a Comment