Take Control of the WordPress Auto-Paragraph Tag

WordPress has a function called wpautop() that very kindly “autowraps” and adds paragraph tags (<p></p>) around elements of your content.  While this is extremely helpful for users, those that may be more unfamiliar with HTML and those that only want to use WordPress as a content publishing platform.  Those users that desire more control over the structure of the site may want to manage what elements exist or don’t exist in the content structure.

For a long time, WordPress users have had access to filtering out the wpautop() function by adding a simple line of code to their functions.php file of their active theme folder.

remove_filter( 'the_content', 'wpautop' );

But the above code can also leave users with an unintended issue.  This code snippet will remove ALL the auto-inserted paragraph tags.  To truly have control over when and where this wpautop() function interacts with content, we need to be able to manage this on an individual post/page level.

This is where WordPress’ Custom Fields come to the rescue. If you do not see a metabox on your post/page editor screen, you may need to enable the metabox to be visible from within the Screen Options located inside the drop-down tab in the upper right of your screen window.

Once you enable Custom Fields by checking the box, you will see a new metabox appear in the edit post/page area.

custom fields

From within this metabox, we can press the “Enter New” link to create a newly named unique field. In the “Name” input box enter the text wpautop.  (Side Note: After your initial time of adding the wpautop value to the Name input box, all proceeding attempts to add this field can be done through the drop-down box.) Then in the corresponding value box located to the right, enter the text false.  Now the metabox name and value should look like the following image.

Once you save your post/page this Custom Field value is now stored in the postmeta of this specific post ID… which means that in our code we can identify whether a certain post ID has a Custom Field that is declaring wpautop => false.  We do this “conditional check” in a small function that is placed in the functions.php file of your active WordPress theme folder.

function wpautop_filter_control( $content ){
    if ( get_post_meta( get_the_ID(), 'wpautop', true) == 'false' ){
        return $content;
    } else {
        return wpautop($content);
    }
}
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop_filter_control' );

The above code snippet will now check to see whether the active post ID has the wpautop custom field value set to false.  If it finds that particular custom field setting exists, it will return the_content without the wpautop() function running on the content.  If that exact custom field setting is not found, then the_content is returned after running wpautop on the content.

Now you have complete control over disabling wpautop() function on a per-post basis.

Leave a Reply

Your email address will not be published. Required fields are marked *