2

I'm using a theme that use the_excerpt() function to show the excerpt of the post.

All posts have not set custom excerpt, so the_excerpt() returns a piece of post content.

In some post first paragraph contains a <br>, for example:

<p>My new question is:<br>why words are not separated by a white space?</p>

The text rendered is:

My new question is:why words are not separated by a white space?

Following this post I implemented this solution:

function my_excerpt($text = '', $raw_excerpt = '') {
    add_filter('the_content', 'my_content', 6);

    // get through origin filter
    $text = wp_trim_excerpt($text);

    remove_filter('the_content', 'my_content', 6);
    return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt');
add_filter( 'get_the_excerpt', 'my_excerpt');

function my_content($text)
{
    return str_replace( '<br>', ' ', $text );
}

And this works, but I have two questions:

  1. Why the_excerpt() doesn't substitute <br> with space?
  2. Is there a better way to achieve this result?

Also, I'm quite new to wordpress development, any suggestions to improve my code are welcome.

Update: I found that is reported this issue, but unfortunately is still open.

4
  • 1
    Why would it substitute with a space? That would break things in some situations – Tom J Nowell Oct 30 '20 at 15:53
  • 1
    @TomJNowell In the page that show article with a little piece of text (like categories or post per years) sentence are showed in a messed way, like the example above. So I want that when the excerpt is automatically generated from content, it is rendered in a correct way. Did you think that my code can break things? could you explain when and why? Thanks – Lety Oct 30 '20 at 16:00
  • 1
    Whitespace would make sense to me too. I think it'd be better to change wp_strip_all_tags to add the whitespace though and leave the_excerpt alone. There's probably other cases e.g. if there was no whitespace in the markup when one paragraph ends and another starts e.g. 'abc</p><p>def', and other versions of br e.g. <br/>, <br /> - or with any attribute too really - but simple <br> is probably the most common.. – Rup Oct 31 '20 at 11:29
  • @Rup thanks. I see where is the problem. wp_strip_all_tags use php function strip_tags that remove alla html tag and generate messy excerpt both in case of br and in case of </p><p>. Probably classic editor put white space before br and at the end of p tag. I've used the_content filter that is applied just before text manipulation in wp_trim_excerpt. I should improve my str_replace to keep other br as you point out. Could you help me? Something like: <br>|<br/>|<br />? I'm not very good with regular expressions. – Lety Oct 31 '20 at 14:54
2

wp_trim_excerpt() calls wp_trim_words() which applies wp_strip_all_tags() to the excerpt. The <br> tag is removed and not replaced with anything.

Not sure if this is feasible, but having a space after a colon is grammatically correct, and adding it to the original content would not affect the HTML, e.g.:

<p>My new question is: <br>why words are not separated by a white space?</p>

3
  • thanks for the reply, although it might be a way around this, I should always check that there is space before SHIFT + RETURN, because in the editor space at the end of sentence is not visible – Lety Oct 30 '20 at 15:56
  • You could always use &nbsp; for the space too – Dave Romsey Oct 30 '20 at 16:05
  • In the gutemberg editor I can't use it, I should edit HTML, but often writers doesn't know it... anyway +1 for your effort, thanks – Lety Oct 30 '20 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.