How to stop removing <p> & <br> tags in wordpress editor without any plugins?

I have use WordPress editor but I don’t want to remove all extra <p></p> and <br> tags.

How to stop removing p & br tags in wordpress editor without any plugins

Read More

Anyone please help.

Related posts

3 comments

  1. Recently I tried to solve same issue and found this https://www.leighton.com/blog/stop-tinymce-in-wordpress-3-x-messing-up-your-html-code

    A solution that works
    After making do with a setup that was marginally better than the out-of-the-box way of working for 6 months, we found that the JavaScript modification method above no longer worked due to changes in the core TinyMCE.js file introduced with recent versions of WordPress 3.x, and that there had been an easy, clean and highly effective solution under our noses all the time that essentially replicates our old JavaScript method but using PHP and WordPress hooks to change the parameters TinyMCE uses when it is initiated.

    This short bit of PHP code should be put into your themes functions.php file

    function override_mce_options($initArray) {
        $opts = '*[*]';
        $initArray['valid_elements'] = $opts;
        $initArray['extended_valid_elements'] = $opts;
        return $initArray;
    }
    add_filter('tiny_mce_before_init', 'override_mce_options');
    

    … and voila! no more messing with your source code when saving or switching views

  2. I have a solution how to stop removing <br> tags or double (<br><br>) line breaks.

    1. Make changes to your file /wp-content/themes/your_theme_name/functions.php

    Add 2 lines to the top of your functions.

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

    This will turn off wpautopop function.

    1. Make changes in file /wp-includes/formatting.php in function wpautop.

      A) Change function wpautop( $pee, $br = true) to function wpautop( $pee, $br = false).

      This will additionally turn off wpautopop function from all places.

      B) Change $pee = preg_replace('|<brs*/?>s*<brs*/?>|', "nn", $pee); to

      $pee1 = $pee;
      $pee = preg_replace('|<brs*/?>s*<brs*/?>|', "nn", $pee);
      $pee = $pee1;
      

      This will prevent the system from removing double <br> tags. (I know the code is strange but simple //$pee doesn’t help here because of ?> tag).

      C) Change $pee = preg_replace("/nn+/", "nn", $pee); to //$pee = preg_replace("/nn+/", "nn", $pee);

      This will prevent the system from removing multiple line breaks.

      D) Change this:

      $pee = preg_replace('!<p>s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
      

      to that:

      //$pee = preg_replace('!<p>s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
      

      This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

      E) Change this:

      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*</p>!', "$1", $pee);
      

      to that:

      //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*</p>!', "$1", $pee);
      

      Pretty the same: This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

      F) Change this:

      $pee = preg_replace('!<br />(s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
      

      to that:

      // $pee = preg_replace('!<br />(s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
      

      This will prevent the system from removing <br> at the end of the block.

      G) Change this:

      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*<br />!', "$1", $pee);
      

      to that:

      //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)s*<br />!', "$1", $pee);
      

      This will prevent the system from removing <br> after an opening or closing block tag.

    Hope it will help! And read comments in this file – they will help you to understand what you need to turn on or turn off.

  3. I’ve had the same problem and i’ve been using the no breaking space tag to ‘fix’ it. The content in my text tab of the editor will look something like this:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras luctus placerat risus, vel suscipit nibh cursus sed. Donec sit amet urna maximus enim malesuada eleifend et sit amet nibh.
     
    Fusce accumsan justo id orci suscipit pulvinar. In feugiat dolor id blandit luctus. Phasellus placerat enim felis, quis elementum risus ultricies sagittis.

Comments are closed.