How to remove automatic paragraph formatting for pages ONLY, not posts (WordPress)

I'm already familiar with this little trick for removing automatic paragraph formatting in WordPress:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

      

... however adding this to functions.php removes paragraphs for the entire site. This is not what I want as the client needs to be able to edit the changes on their own (paragraph formatting really helps them in messages).

If auto paragraph inserts are especially harmful on client home page where there are javascript snippets. So ideally I would like to disable auto p formatting for this page only, or all pages if needed, and leave the messages alone.

Any ideas? If necessary, I can provide additional information.

Thanks in advance!


Edit:

Plugins I've tried: Php Exec, Raw HTML, Disable WordPress Autop, PS Disable Auto Formatting, Toggle wpautop

+3


source to share


3 answers


You should check if the rendered template is a page using is_page () and then run the filter additionally. We connect to 'wp_head'

so that we can run the check before it gets called the_content

.

Example:



function remove_p_on_pages() {
    if ( is_page() ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );
    }
}
add_action( 'wp_head', 'remove_p_on_pages' );

      

+9


source


You can add a custom category to the page you want, then use the category id to disable wp_autop



//no paragraph
function no_auto_paragraph( $atts ){

  $cats = get_the_category();
  $cat  = $cats[0]->cat_ID; 

  if ($cat == 7 ){ //in my case the category is 7
    remove_filter( 'the_content', 'wpautop' );
  }

}

add_action( 'wp_head', 'no_auto_paragraph' );

//no_auto_paragraph END

      

0


source


I suggest adding it to your theme's home.php file if needed. Ideally, just add it to the theme javascript file, or otherwise separate the content (master page content) from the controller (your javascript) (for example, including the JS file on the master page only).

-1


source







All Articles