Disable automatic p and br tags in the wordpress editor – and do it as early, as you can!

Why you should disable them as early, as you can

I don’t like visual HTML-editors, because they always mess up your HTML. So the first thing, that I’ve done in my wordpress-profile, was checking the check-box Disable the visual editor when writing. But today I found out, that this is worth nothing. Even when in text-mode, wordpress is adding some <p>- and <br>-tags automagically and, hence, is automagically messing up my neatly hand-crafted HTML-code.

Fuck wordpress! (Ehem – sorry for that outburst)

But what is even worse: after really turning off wordpress’s automagically-messup-functionality, nearly all my handwritten <p>-tags were gone, too. So, if you want to turn of automatic <p>- and <br>-tags, you should really do it as early, as you can. Otherwise, you will have to clean up all your old posts afterwards like me. TI’ve lost some hours with usless HTML-editing today, because of that sh#%&*!

How to disable them

The wordpress-documentation of the build-in HTML-editor links to this post, which describs how to disable autmatic use of paragraph tags. Simple open the file wp-includes/default-filters.php of you wordpress-installation and comment out the following line:


addfilter('the_content', 'wpautop');

If you are building your own wordpress-theme – like me – you alternatively can add the following to the functions.php-file of your theme:


remove_filter('the_content', 'wpautop');

Why you should disable automatic paragraph tags

For example, I was wondering a while, where all that whitespace in my posts were coming from. Being used to handcraft my HTML, I often wrote one sentence per line, or put some empty lines inbetween to clearly arange my code. There comes wordpress, messing everything up by automagically putting every sentence in its own paragraph, because it was written on its own line and putting <br> inbetween, to reflect my empty lines.

But even worse, wordpress also puts these unwanted <p>-tags arround HTML-code, that breaks because of it. For example, I eventually found out about this auto-messup functionallity, because I was checking my blog-post with a html-validator and was wondering, why the validator was grumping about a <quote>-tag inside flow content, which I’ve never put there. It turned out, that wordpress had put it there for me…

Comments / Questions

  1. :)😁 says:

    Hey thank you for Guidence. I am also messing around with same problem but unfortunately i searched every post on google regarding this.

    I applied all the methods like

    Themes>functions.php

    and

    wp-includes>default-filters.php

    But nothing worked. Whenever i go to post code editor and embed my beautiful Code and as soon as i click on preview button. Those Nasty cranky tags [ } appear from somewhere.

    How ever when i cleared all the spaces of my code. It worked but it makes it hard to debug code and adding new lines of code.

    Please reply as soon as possible SIR !

  2. Pihu Gupta says:

    Thanks, Actually I wants to adding a style code with using some multiple Input & Id, Values in the Single Posts. But I just fix this. Thank You So Much Sir.

  3. Richard says:

    The Raw HTML plugin offers a “Disable automatic paragraphs” function.

    Works for me to enable the Ordnance Survey OS API, for maps and POI markers.

    1. Kai Moritz says:

      Hi Richard,

      thank you for your hint.
      I will take a look at it.
      I was not able to find that plugin, when I first stumbled accross the problem.
      So, it is good, this probably easier alternative is pointed out here.

  4. sofian says:

    Hello, thanks for this post, I am in the same situation, I find WP’s HTML Editor behaviour a joke.

    Now if I understood correctly, the use of remove_filter(‘the_content’, ‘wpautop’); should result in my p-tags to stay when i switch to visual and back, but they dont.

    Is this the effect that that code brings to you? Can you normally work now in your HTML Editor with all tags? Are they getting lost if you switch to visual and back?

    Thank you
    Sofian

    1. Kai Moritz says:

      Hi Sofian,

      I think, that that would be the effect of my code.
      Unfortunatly, I cannot test this now, because I turned of the visual HTML-editor in my wordpress-installation, because I do not need it.
      So, I think you will get what you are looking for.
      But it would be nice, if you could report back, if my trick worked for you, so that future readers can profit from your work.

Leave a Reply to Kai Moritz Cancel reply

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