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 
 */
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. Toggle wpautop
  2. Disable Automatic P Tags
  3. Empty P Tag

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


Source link

Leave a Reply

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