Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#56120 new enhancement

WordPress should add a space character on every possible "wrap point" in a post title when building "html title", "og:title" and so on

Reported by: pezcurrel's profile pezcurrel Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Formatting Keywords: 2nd-opinion
Focuses: Cc:

Description

In WordPress it is possible to use html tags inside posts title. This is very nice: for example, it allows to use italics, and to make posts titles wrap where one wants. When the latter is the case, though, the titles get displayed good in the post, but not in the browser's window title (that usually the browser builds after the <title>Foobar</title> html entity), and not in the link previews that one can get by linking a post into a social network post (that usually have their title based on the <meta property="og:title" content="Foobar" /> entity).
For example, a post title like First line<br>second line<br>third line will result in a <title>First linesecond linethird line</title> entity and in a <meta property="og:title" content="First linesecond linethird line" /> entity, that will result in ugly and difficult to read browser window and link previews titles.
This could be avoided by changing WordPress code so that it added a space character on every possible "wrap point" (<br>, </p>, </div> and so on) in a post title when building "html titles", "og:title"s and the likes.

Change History (2)

#1 @sabernhardt
2 years ago

  • Component changed from General to Formatting
  • Keywords 2nd-opinion added

If the title has a line break element without a space, that would affect the URL slug, too.

However, if I add spaces,
First line <br>second line <br>third line
results in a title of
<title>First line second line third line &#8211; Example Site</title>
and a URL of
https://example.com/first-line-second-line-third-line/

To be thorough, the line break could match <br (the tag might be <br>, <br/> or <br />—and it could have an attribute).

I added '2nd-opinion' because I think there might be a reason not to make this change for everyone, but I have not thought of an example of an inappropriate case (perhaps in another language). Ideally, users would add the spaces as needed and get them in the heading as well as the title and URL, though this is a predictable thing to correct.

#2 @costdev
2 years ago

To be thorough, the line break could match <br (the tag might be <br>, <br/> or <br />—and it could have an attribute).

This pattern and a trim() for leading/trailing <br> tags seems to do the trick: /(\s)?<br(.*?)>(\s)?/. 3v4l.

I'm not sure of inappropriate cases either, so I'd be interested in second opinions on this.

Last edited 2 years ago by costdev (previous) (diff)
Note: See TracTickets for help on using tickets.