WordPress wpautop() Filter
Put simply, the wpautop() filter changes double line-breaks in the text into HTML paragraphs (<p>…</p>).
This can, however, sometimes have annoying side affects. For example, it adds paragraph tags around image and script tags. This can have strange affects on page layouts, and may break scripts. Sometimes it may be desirable to disable the wpautop() filter; and prevent it’s functionality from occurring.
Standard Disable wpautop()
This has long been a heated issue with WordPress users. While beginners to WordPress may never notice this “behind the scenes” filter; it will soon become apparent when creating more advanced content.
WordPress provides a simple function which removes the wpautop() filter from filtering the post content. The following function can be added to a child themes functions.php file; disabling the wpautop() function:
remove_filter( 'the_content', 'wpautop' );
Affects of Disabling the wpautop() Filter
Let’s look at exactly how this filter performs; and how it affects post content.
The following two screen images show a post created without disabling the wpautop() filter from the editor view (left) and the website view (right).
Now, let’s remove the wpautop() filter; and see what happens.
The following two screen images show the same code as above, but with the wpauto() filter disabled; editor view (left) and website view (right).
Notice how the lines in the Website view have been pushed together? What happened? To answer this question, we have to look at how the content editor code is output to the browser.
Let’s compare the two “Website Views” from above. The left screen image will show the html from the top example; and the right screen image the bottom example.
We can determine the left screen image is showing each line wrapped in a p tag; while the image on the right shows no p tags wrapping our lines.
This makes sens with what we are seeing in the initial examples. If p tags are being added by WordPress (default); we see the results from the original top example, and the HTML example on the left.
If we remove the wpautop() filter; we get the results of the original bottom example, and the HTML example on the right.
Okay, I Want to Remove the wpautop() Filter
Now that we know how this wpautop() filter affects our post content when rendered on the website, we may wish to disable the functionality.
Yes, it will. And this can be a big problem. If we just throw the default WordPress function into our child theme, it will affect all of the posts and pages (including those already created).
This means you will need to manually go back and add the p tags to all lines in any current content. If you have hundreds (or even dozens) of existing articles, this may not be a viable option.
WordPress, yet again, will allow us around this issue by using WordPress Conditional tags. These tags can be used in conjunction with the original wordpress function to only allow the wpautop() filter to be removed on certain pages.
Whoa!! Isn’t there an easier way?
WP Edit Pro Disable wpautop()
Yes, there is a much easier way.
First, we need to enable the “Disable wpautop()” option (yes, double-negative). Visit the plugin options page -> Posts/Pages Options tab.
WP Edit Pro wpautop() Post Types
WP Edit Pro also allows the administrator to select which post types will display the option to disable the wpautop() filter.
The two standard WordPress post types are “Posts” and “Pages”. However, many plugins and themes will create additional post types. WP Edit Pro will search for additional post types, and allow them to be toggled as well.
Visit the admin panel -> Settings -> Writing. Scroll down, and find the “Toggle wpautop” section.
Next, visit any post or page (or custom post type) where the wpautop() option has been allowed. Directly to the right of the content editor (near the “Save” or “Publish” buttons), there will be a new option to “Disable wpautop”.
Checking the box, will disable the wpautop filter for ONLY that article.
Remember, WordPress added this function for a reason. WordPress “assumes” the majority of the users are beginners. Therefore, they built this function to search the content; and adjust broken p tags. Disabling the filter also disables this ability to “fix” broken p tags.
A few observations I have made while disabling the wpautop() filter.
It is important to remember disabling the wpautop() filter requires the article author to manually enter p tags around the lines of content. If the p tags are used improperly; it may break the layout of the page.
Switching Visual and Text Views
When disabling the wpautop() filter, it becomes necessary to manually add p tags. If the p tag is added without a class or an id attribute, it will be stripped out of the content editor when switching views.
Disabling the wpautop() filter is definitely one way to remove the pesky stripping of p tags from the content. However, remember the implications. I found that it completely changed the way I create content with WordPress.