Disable automatic paragraphs in WordPress posts

2 minutes read

WordPress automatically adds paragraph tags (<p>) in the posts wherever it notices double line-breaks. For example, if you write one line and press the return key twice, it considers you want to add a paragraph there, and then suitable tags will be added when you publish or preview the post.

Auto paragraphs added by wpautop()

You don’t see the tags added in the post editor, but they will show up in the markup when you view the source of your post.

The same thing happens with the WordPress image captions when your insert media right from the ‘Insert Media’ dialog. You may see the same thing happening with couple of more things in WordPress.

This automatic feature is quite handy as it saves you from adding paragraph tags or selecting the paragraph option again-and-again in the Visual mode. But you may want to turn it off, I don’t really know your reason to do that, but I did that to show the demo pages properly.

How?

WordPress function wpautop(); (see at Codex) is responsible for adding paragraph HTML across the ends of your text followed by 2 line-breaks. To avoid auto <p>s, we have to stop this function from doing this.

Provided that you have your reason to turn auto paragraphs in WordPress completely, here’s the single line of code you’ve to add in the functions.php file of your theme and then save it:

remove_filter( 'the_content', 'wpautop' );

For excerpts?

A slight change needed to siege wpautop() from modifying the excerpt:

remove_filter('the_excerpt', 'wpautop');

You may simply remove or comment out this line if you’ve changed your mind.

Remove auto paras only for CPTs

If you want to disable it for Custom post type only, add this a-bit-longer code in your theme’s functions.php and save the changes:

add_filter( 'the_content', 'disable_wpautop_cpt', 0 );
function disable_wpautop_cpt( $content ) {
  'your_cpt_slug' === get_post_type() && remove_filter( 'the_content', 'wpautop' );
  return $content;
}

All you need is to replace your_cpt_slug to your custom post type slug. I hope you found this useful. Let me know if you have any questions regarding above pieces of code.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

There are times when you need to disable comments on your WordPress site. There are two cases contextual to disabling WordPress comments: Disabling comments for individual posts and pages Disabling comments completely Disabling comments for individual WordPr...
Filtering posts by year on WordPress can be achieved by utilizing the built-in functionality of the WordPress CMS. This feature allows you to sort and display posts based on their publish year in a customized manner. Here is a step-by-step guide on how to filt...
To select posts from a specific taxonomy in WordPress, you can use the WP_Query class to create a custom query that retrieves posts based on the taxonomy term. You would first need to get the term ID of the taxonomy you want to query, and then use that term ID...