Paragraphs are not stripped but turn into line-breaks
Closed this issue ยท 4 comments
When SEOMate outputs <title>
and <meta desc>
, tags like <br>
, <strong>
, '<em>
, ... are properly stripped.
Paragraphs, however, turn into line-breaks, both for <title>
and <meta desc>
.
This is particularly problematic when using a rich text fields.
Example
Content of variable altTitle
(a rich text field):
<p>A page title with<p>
<p>a new <strong>paragraph</strong></p>
outputs
<title>A page title with
a new paragraph</title>
config/SEOMate.php
...
'fieldProfiles' => [
'standard' => [
'title' => ['seoTitle', 'altTitle', 'title'],
'description' => ['seoDescription', 'firstParagraph']
],
],
...
Those linebreaks are newline characters (i.e. \n
) added by Redactor after every </p>
tag, and they persist because the strip_tags()
function doesn't remove control characters.
FWIW, newline characters in <title>
or <meta>
tags isn't considered invalid HTML, and Google/Facebook/whatever seems to handle them just fine, too. Generally, newlines simply render as a single space where appropriate, which is actually a good thing โ because if we would simply remove them, the <title>
in your example would render as A page title witha new paragraph
๐ฌ
We have debated if SEOMate should replace closing/opening paragraph tags with literal spaces, but so far we've opted to just keep the newlines and avoid the potential edge cases involved in doing that.
That said, at this point Redactor isn't the only field type outputting rich text, and it's not even the recommended, first party one anymore. I took a look at CKEditor, and it turns out that CK doesn't produce the newlines after paragraphs... which means that if a CKEditor field is used for SEOMate metadata, paragraphs will be joined together without spaces โ which IMO is a bigger problem than Redactor fields having newlines retained. But in any case, perhaps it's time to revisit the problem and figure out a solution that works well for any and all rich text field types.
TL;DR โ maybe the best call after all, is if SEOMate a) removes any newlines and b) adds a literal space between stripped paragraphs.
Thanks for clarifying!
And you are right, as it turns out, the <p>
tags weren't the problem, but rather the accompanying \n
inserted by redactor.
I was able to work around this by adding something like this to config/seomate.php
:
'tagTemplateMap' => [
'title' => '<title>{{ value|replace({"\n": "", "\r": ""}) }}</title>',
]
We've decided to address this by replacing all control characters (including newlines) by a literal space.
This change will be in the next SEOMate release.
SEOMate v. 2.2.0 is out now with this change.