5

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

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

...however adding this in functions.php removes paragraphs for the entire site. This is not what I want since the client needs to be able to make edits themselves (the paragraph formatting really helps them out on posts).

Where the auto paragraph insertions are particularly damaging is on the client's home page, where there are javascript snippets. So ideally, I would like to disable auto p formatting for this page alone, or all pages if necessary, and leave posts alone.

Any ideas? I can provide more information if needed.

Thanks in advance!


Edit:

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

1
  • tried adding above code to functions.php and different theme pages, and tried a variety of plugins that can't seem to shake off the extra p tags, including Disable WordPress AutoP, Toggle AutoP, PHP exec, and a few others. I'm assuming it's just a couple lines of php code in funtions.php where I tell wordpress to remove auto-formatting for pages only, or only one page maybe? Thoughts? Oct 26, 2014 at 6:14

2 Answers 2

13

You should be able to check if the template being rendered is a page using is_page(), and then optionally run the filter. We hook into 'wp_head', so that we can run the check before the_content is called.

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' );
5
  • I added this to the bottom of my functions.php file and returned no luck. I also tried adding different code to grab the page by ID with no luck either: function remove_p_on_pages() { if (get_the_ID()==94) { remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' ); } } add_action( 'wp_head', 'remove_p_on_pages' ); Oct 26, 2014 at 8:22
  • 1
    I've tested my code, and it does work. It only works for true pages. You can optionally replace the is_page with is_front_page() to run it on the front page only.
    – rnevius
    Oct 26, 2014 at 8:29
  • Sorry man, it's just not working for me. I bet in another theme it would work fine since the code looks solid. I tried adding is_front_page is_page and a few others and it won't work. The only thing that seems to be going through is the original remove_filter bit. I've emailed the theme developers on this too and really appreciate you trying to help out, thank you! Oct 26, 2014 at 8:48
  • What do you mean the original remove_filter is going through? You should be using the above function in place of the other remove_filter() functions from your original question. Good luck.
    – rnevius
    Oct 26, 2014 at 8:49
  • Yes I did this - I removed the original remove_filter() line when trying out the new code block above (is_page). I just meant the only "fix" at this point is using the original code in OP, which as you know is not optimal. Tx again, sorry for the any confusion. Oct 26, 2014 at 8:56
0

You can add custom category to desired page 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.