How can I keep WordPress from stripping out my <p> and <br> tags?

WordPress strips out <p> and <br> tags when switching between the visual and the text editors. Well, to be specific, it doesn’t actually strip them out of the content, but it does not display them in the text editor view. Is there a way to stop it from hiding those tags?

I know about the tinymce advanced plugin, but is there a way to stop this behavior on a per-post/per-page basis without having to install the plugin?

Related posts

Leave a Reply

2 comments

  1. If you add a meaningless data attribute to the <p> and <br> tags, they will continue to show.

    Example

    When switching from text to visual editor and back, the following text

    <p>Some paragraph text <br> and a second line.</p>
    

    becomes

    Some paragraph text
    and a second line.
    

    However,

    <p data-x>Some paragraph text <br data-x> and a second line.</p>
    

    makes the tags remain visible:

    <p data-x="">Some paragraph text <br data-x="" /> and a second line.</p>
    

    This is useful on pages with more complex layouts that can shift around when the tags get stripped.

  2. If the post is custom post type, you can add meta box with add_meta_box and there you can initialize your own editor with wp_editor which can be customized. For example you can pass settings to the tinymce like force_p_newslines which should force every new line to start with new paragraph