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
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.
It’s not elegant solution, but maybe it will solve the problem changing
<br>
to paragraph<p>
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 inwpautop( $pee, $br )
like thisThis 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 againlet’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
Note that I’m assuming the site in question is running WordPress 5.xx.
The expected behavior is not that, but this one (* note the
<br />
which is not<br>
):I.e. One paragraph for the two lines, with a
br
tag inside; and not one paragraph for each line:Single line break after the “baz.”:
After wpautop():
Double line breaks after the “baz.”:
After wpautop():
Here’s why which is an excerpt from the official function reference for
wpautop
: (reformatted for brevity)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):And then just view the default “Hello World” post. (Or any pages having The Loop, actually..)
Now if you want this output:
Try this:
But that’s just a simple example and would likely fail with complex or invalid HTML.