Disable WordPress autop

By  on  

Back when I first started this blog, and in turn using WordPress, the most annoying part of blogging was WordPress' auto-formatting.  WordPress would strip out custom attributes, mess with my DIV and paragraph structure, and just generally raise havoc.  It bugged me so much that since the inception of this blog, I've been creating the HTML for blog posts myself.  That did, however, require turning off autop -- the filter that normally formats post content.  To turn off the autop filter, simply add this PHP snippet to your theme's functions.php file:

remove_filter('the_content', 'wpautop');

This tiny remove_filter call could save you a word of trouble with formatting that seems out of your hands.  If your WordPress blog uses multiple themes and you'd prefer to add this functionality as a plugin, you can place the following within a PHP file to be placed in the /wp-content/plugins directory:

/* 
* Plugin Name: Disable autop
* Author: David Walsh
* Author URI: https://davidwalsh.name/
* Plugin URI: https://davidwalsh.name/disable-autop
* Version: 0.1
* Description: Disable WordPress' horrible automatic formatting
*/

remove_filter('the_content', 'wpautop');

This would also be helpful if you would prefer to use Markdown to write posts, as you could convert the Markdown with a different plugin or by yourself within each theme file.

Recent Features

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    JavaScript Canvas Image Conversion

    At last week's Mozilla WebDev Offsite, we all spent half of the last day hacking on our future Mozilla Marketplace app. One mobile app that recently got a lot of attention was Instagram, which sold to Facebook for the bat shit crazy price of one...

  • By
    Fixing sIFR Printing with CSS and MooTools

    While I'm not a huge sIFR advocate I can understand its allure. A customer recently asked us to implement sIFR on their website but I ran into a problem: the sIFR headings wouldn't print because they were Flash objects. Here's how to fix...

Discussion

  1. Je Suis

    Now if you can figure out a way to turnoff the wordpress magic quotes function they force you to use, I’ll tell everyone how awesome you are.
    Well I already do that anyway

  2. I take it you would like my guest post to be done in HTML after all? ;)

    • Hahahaha. Nah, I’ll format it myself! :)

  3. Alex

    I needed something like this 2 years ago, I simply used a plug-in if I’m not mistaken. It’s a frustration indeed.

  4. It is annoying to developers, but is quite a handy feature when your clients are entering the content.

    We can not presume that everybody have the knowledge/time to format every single post by hand, so it is handy sometimes.

    • Intelligently inserting p tags to wrap content from the Visual tab of the editor is okay for non-technical clients. Injecting p tags into the Text (read: code) section of the editor is not. It forces technical users to stoop to kludgey workarounds like editing functions.php, or superfluous div-wrapping elements to prevent WP from p-wrapping them.

  5. You know same thing could be done with the_excerpt and comment_text filters

  6. Big D

    Another option which worked for me…

    Just open up formatting.php in the wp-includes folder, find the wpautop function (bout line 174) and comment out everything within the function, except “return $pee;” at the very end… so it should look like this:

    function wpautop($pee, $br = 1) {
    	/*
    	if ( trim($pee) === '' )
    		return '';
    	$pee = $pee . "\n"; // just to make things a little easier, pad the end
    	$pee = preg_replace('|\s*|', "\n\n", $pee);
    	// Space things out a little
    	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
    	$pee = preg_replace('!(]*>)!', "\n$1", $pee);
    	$pee = preg_replace('!()!', "$1\n\n", $pee);
    	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    	if ( strpos($pee, '<object') !== false ) {
    		$pee = preg_replace('|\s*]*)>\s*|', "", $pee); // no pee inside object/embed
    		$pee = preg_replace('|\s*\s*|', '', $pee);
    	}
    	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    	// make paragraphs, including one at the end
    	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
    	$pee = '';
    	foreach ( $pees as $tinkle )
    		$pee .= '' . trim($tinkle, "\n") . "\n";
    	$pee = preg_replace('|\s*|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    	$pee = preg_replace('!([^<]+)!', "$1", $pee);
    	$pee = preg_replace('!\s*(]*>)\s*!', "$1", $pee); // don't pee all over a tag
    	$pee = preg_replace("|(<li.+?)|", "$1", $pee); // problem with nested lists
    	$pee = preg_replace('|
    ]*)>|i', "
    ", $pee); $pee = str_replace('
    ', '
    ', $pee); $pee = preg_replace('!\s*(]*>)!', "$1", $pee); $pee = preg_replace('!(]*>)\s*!', "$1", $pee); if ($br) { $pee = preg_replace_callback('/<(script|style).*?/s', '_autop_newline_preservation_helper', $pee); $pee = preg_replace('|(?<!)\s*\n|', "\n", $pee); // optionally make line breaks $pee = str_replace('', "\n", $pee); } $pee = preg_replace('!(]*>)\s*!', "$1", $pee); $pee = preg_replace('!(\s*]*>)!', '$1', $pee); if (strpos($pee, '<pre') !== false) $pee = preg_replace_callback('!(]*>)(.*?)!is', 'clean_pre', $pee ); $pee = preg_replace( "|\n$|", '', $pee ); */ return $pee; }
    • Dude – really??? How is that easier?

    • Jester

      And next time you update your WP install you will need to do it againl

  7. Jeff

    Using your last code snippet to create a plugin caused an error regarding ‘excessive unexpected output’ or some such thing when I placed the resulting .php file in my plugins folder and activated the plugin.

    I could also see your authorial information pasted at the top of my wp site while the plugin was enabled. Although wordpress warned me that it could break my site, other than the aforementioned text at the top my site continued to work fine, although it didn’t appear to disable the autoformatting.

    I’m on wordpress 3.6– do you have any ideas as to why it didn’t work?

  8. Disabling the autop feature definitely works, but when client tries to create blog posts we can’t use paragraph formatting. Anyone have an idea how to both disable the feature, but re-enable it on posts?

  9. THANK YOU! I was in the process of writing a php function to do just this when I ran into this. You definitely saved precious time!

  10. 5 stars! Thanks David.

  11. Wow, guys, you are far more technical than I.

    I am having this problem but need to know where I can find the /wp-content/plugins directory: in my dashboard. Is it under the Plugins section? If so, can you advise exactly where?

    Also, is it possible to use a particular html code at the end of each paragraph where I am having this issue?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!