skip to Main Content

This is not asking how to turn off auto-paragraph formatting

Pretty much the opposite – I am using wpautop, but it’s not doing paragraphs like I think it should.

The problem I’m having is my entire post is being wrapped as one <p> and then <br /> is being used for all the line breaks.

This is a client site that’s been moved around a few times, and most recently imported posts from an export of the last version of the site.

<br /> tags are not present in the database fields. I can only guess that there is some inner working of wpautop that’s lining up with some sort of special character line break that has been accidentally replaced in all the migrating.

Would anyone have a definitive answer to what this is? Bonus points if you can show how to fix it without opening, reformatting, and re-saving hundreds of posts manually.

For clarity, I want there to be the normal paragraphs instead of the br I’m getting now

More info

When copy-pasting post content from PHPMyAdmin to Notepad++, it shows (CR)(LF) at the end of each line. Perhaps I am wrong about the cause, as this is what it should be, correct?

Example usage

Doing:

$product->get_description()

yields, as expected:

Happy birthday never tasted this good.
Topped with a hand piped personal message. These cupcakes are perfect for any birthday celebration and will put a smile on that special someone's face. A mix of chocolate and vanilla; like any good birthday cupcakes these are loaded with sprinkles!

Which exactly matches the field in the database:

Happy birthday never tasted this good.
Topped with a hand piped personal message. These cupcakes are perfect for any birthday celebration and will put a smile on that special someone's face. A mix of chocolate and vanilla; like any good birthday cupcakes these are loaded with sprinkles!

But doing:

wpautop($product->get_description())

yields:

<p>Happy birthday never tasted this good.<br>
Topped with a hand piped personal message. These cupcakes are perfect for any birthday celebration and will put a smile on that special someone's face. A mix of chocolate and vanilla; like any good birthday cupcakes these are loaded with sprinkles!</p>

Trying to get wpautop to do this, which I believe is expected behavior:

<p>Happy birthday never tasted this good.</p>
<p>Topped with a hand piped personal message. These cupcakes are perfect for any birthday celebration and will put a smile on that special someone's face. A mix of chocolate and vanilla; like any good birthday cupcakes these are loaded with sprinkles!</p>

It’s been weeks I’ve been stuck on this. Even suggestions leading to better debugging would be acceptable.

3

Answers


  1. If you set the value of $br to false you will remove line breaks in paragraphs. If set to true, any line breaks left after the paragraph conversion are converted to HTML <br>.

    I do not know if it solves your problem, but it is worth checking.

    wpautop($product->get_description(), false);
    

    It’s not elegant solution, but maybe it will solve the problem changing <br> to paragraph <p>

    str_replace("<br>", "</p><p>", wpautop($product->get_description()));
    
    Login or Signup to reply.
  2. wpautop function replaces a new line character with <br> tag which is expected behaviour.
    Here is what you can do

    Step 1. Pass $br as false in wpautop( $pee, $br ) like this

    $br = false;
    
    wpautop($product->get_description(),$br)
    

    This will remove all <br> from your HTML.

    Step 2. Removing <br> will also remove next lines from your HTML too, so you need to add small CSS code to add these new lines again
    let’s say you have wrapped everything with class content
    Like this

    <div class="content"> <?php echo wpautop($product->get_description(),$br); ?> </div>

    Then your CSS code will be

    .content > p { white-space: pre-wrap; }
    
    Login or Signup to reply.
  3. Note that I’m assuming the site in question is running WordPress 5.xx.

    Trying to get wpautop to do this, which I believe is expected
    behavior:

    <p>Happy birthday never tasted this good.</p>
    <p>Topped with a hand piped personal message. These...</p>
    

    The expected behavior is not that, but this one (* note the <br /> which is not <br>):

    <p>Happy birthday never tasted this good.<br />
    Topped with a hand piped personal message. These...</p>
    

    I.e. One paragraph for the two lines, with a br tag inside; and not one paragraph for each line:

    1. Single line break after the “baz.”:

      Foo bar baz.
      Lorem ipsum dolor bla blah.
      

      After wpautop():

      <p>Foo bar baz.<br />
      Lorem ipsum dolor bla blah.</p>
      
    2. Double line breaks after the “baz.”:

      Foo bar baz.
      
      Lorem ipsum dolor bla blah.
      

      After wpautop():

      <p>Foo bar baz.</p>
      <p>Lorem ipsum dolor bla blah.</p>
      

    Here’s why which is an excerpt from the official function reference for wpautop: (reformatted for brevity)

    wpautop( string $pee, bool $br = true )

    …replace double line-breaks with HTML paragraph tags. The
    remaining line-breaks after conversion become <br /> tags, unless
    $br is set to 0 or false.

    You can even see an example here.

    And if you want to be certain, make a fresh install of WordPress with all the default themes and plugins, and then upload this to the wp-content/mu-plugins folder (see Must Use Plugins):

    <?php // File: wp-content/mu-plugins/test.php
    add_action( 'loop_start', function(){
      foreach ( [
        'Single line break'  => 'Happy birthday never tasted this good.
    Topped with a hand piped personal message. These...',
        'Double line breaks' => 'Happy birthday never tasted this good.
    
    Topped with a hand piped personal message. These...',
      ] as $title => $str ) {
        echo '<h2>' . $title . '</h2>';
        echo '<pre>' . esc_html( $str ) . '</pre>';
        echo 'After <code>wpautop()</code>:';
        echo '<pre>' . esc_html( wpautop( $str ) ) . '</pre>';
      }
    } );
    

    And then just view the default “Hello World” post. (Or any pages having The Loop, actually..)

    Now if you want this output:

    <p>Happy birthday never tasted this good.</p>
    <p>Topped with a hand piped personal message. These...</p>
    

    Try this:

    // Note: wpautop() normalizes all line breaks to n and `br` tags to <br />
    $str = wpautop( $product->get_description() );
    
    //$str2 = str_replace( '<br />', '</p><p>', $str );
    // These are optional and the above should already give you what you wanted.
    // So use these two or just the one above.
    $str2 = str_replace( '<br />', "</p>n<p>", $str );
    $str2 = preg_replace( '/<p>n+/', '<p>', $str2 );
    
    echo $str2;
    

    But that’s just a simple example and would likely fail with complex or invalid HTML.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search