How To Disable Automatic Paragraph Tags (With and Without Plugin)

If you are familiar with the formatting of posts and pages in WordPress, you may have already noticed that WordPress separates paragraphs with automatically inserted <p> tags (particularly in WordPress classic editor).

This can sometimes cause trouble. However, a simple code snippet can be inserted to disable this automatic formatting.

WordPress uses the WordPress wpautop function to automatically enclose double-line breaks with a <p> and </ p> tag.

It is used in every type of post (posts, pages, and custom post types) for both content and shortcodes. Consequently, some editing jobs can be quite challenging, such as using a shortcode-driven grid system in a WYSIWYG editor.

Disable Automatic P-tags

Adding the following lines to your theme’s functions.php (ideally in a child theme) will disable the wpautop filter. Once correctly inserted, WordPress will stop automatically inserting the p-tag to posts and excerpts.

remove_filter (‘the_content’, ‘wpautop’);
remove_filter (‘the_excerpt’, ‘wpautop’);

Disable Wpautop on Certain Pages

Use the following code snippet instead if you want to disable this feature on a specific page only. In line 3, specify the URL or the ID of the page you want to disable.

add_filter ('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop ($ content) {
if (is_page ('name of page')) {
remove_filter ('the_content', 'wpautop');
return $ content;
} else {
return $ content;
}
}

Disable Wpautop in Custom Post Types

To disable automatic p-tags for custom post types, you can simply use the following code snippet:

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

Disable Wpautop and List Exceptions

In this code snippet, we list exceptions in the following ways:

/**
 * Allow or remove wpautop based on criteria
 */
function conditional_wpautop($content) {
    // true  = wpautop is  ON  unless any exceptions are met
    // false = wpautop is  OFF unless any exceptions are met
    $wpautop_on_by_default = true;

    // List exceptions here (each exception should either return true or false)
    $exceptions = array(
        is_page_template('page-example-template.php'),
        is_page('example-page'),
    );

    // Checks to see if any exceptions are met // Returns true or false
    $exception_is_met = in_array(true, $exceptions);

    // Returns the content
    if ($wpautop_on_by_default==$exception_is_met) {
        remove_filter('the_content','wpautop');
        return $content;
    } else {
        return $content;
    }
}
add_filter('the_content', 'conditional_wpautop', 9);

Remove Empty Paragraphs Already Added by WordPress

Copy-paste this into your functions.php file (source – GitHub):

/**
 * Remove empty paragraphs created by wpautop()
 * @author Ryan Hamilton
 * @link https://gist.github.com/Fantikerz/5557617
 */
function remove_empty_p( $content ) {
	$content = force_balance_tags( $content );
	$content = preg_replace( '#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content );
	$content = preg_replace( '~\s?<p>(\s| )+</p>\s?~', '', $content );
	return $content;
}
add_filter('the_content', 'remove_empty_p', 20, 1);

Disable Automatic Paragraph Tags with Plugin

The following plugins can be used as well to disable the use of P tags on your website:

  1. Empty P Tag
  2. Disable Automatic P Tags
  3. Toggle wpautop

If you liked this post, please consider sharing it with your friends:

Pinterest

1 thought on “How To Disable Automatic Paragraph Tags (With and Without Plugin)”

Leave a Comment