In WordPress it’s very common to use the wpautop() function. It’s a very handy function for converting plain text into paragraphed & semi-formatted content. However, if you’re using this function alone you could be missing out on a lot of functionality that you could easily get simply by applying the_content filter instead. To show why the_content filter is so much better, let’s take a quick look at what both of these do.
wpautop()
This function is very straightforward – it wraps your line-broken string text in <p> tags. From the repository:
Changes double line-breaks in the text into HTML paragraphs (<p>...</p>).
Pretty basic, right? It helps you to display your string content in very organized fashion.
apply_filter( ‘the_content’ )
This filter runs the content that you feed it through several functions:
- wptexturize – Converts special characters such as … into the proper html display
- convert_smilies – Converts text such as 🙂 into smilies
- convert_chars – Converts more character sinto proper html
- wpautop – Wraps spaced lines in <p> tags
- shortcode_unautop – Stops individual shortcodes from being wrapped in <p> tags
- prepend_attachment – Properly wraps media attachments
- do_shortcode – Runs the shortcode and modifies the output to match
This filter does a whole lot more than just wrap blocks of text in <p> tags – it prepares a string for a whole slew of possibilities and edge cases. In other words, it carries more overhead but ends up being a lot safer to use.
How to use it
Luckily, filters are very easy to use and this one’s among the easiest – take a look at the below gist to see it in action.
All we have to do is feed the filter a string of content, echo or return the content back and voila you have your formatted content!