Reputation: 85
I'm having a slight struggle with the og:title tag on my website. Whenever an article title has an apostrophe and gets shared on Facebook, '
shows up instead of the apostrophe. I've compared my meta tags to a friend's site that successfully shows his apostrophes and the only difference I can find is this:
Code for apostrophes placed in the og:title for my site:
'
Code for apostrophes placed in the og:title for friend's site:
'
I think my site is converting the ampersand after having already converted the apostrophe, and that is making it show up as '
on Facebook.
I'm using K2 for Joomla!, and have asked this question on their forums but I haven't received a single reply in 5 days now.
This is the code I found in the com_k2>views>item>view.html.php file:
$document->setMetaData('og:title', htmlspecialchars($document->getTitle(), ENT_QUOTES, 'UTF-8'));
I'm not php savvy, is there any way I can change this so the code will show up right? If it helps, here's a link to an article with an apostrophe in the title from the site.
Thank you for your time.
Upvotes: 4
Views: 7951
Reputation: 13169
See the documentation for htmlspecialchars and look at the description of when it will convert the single quote (often used as an apostrophe). It will only encode the single-quote if ENT_QUOTES
is set, and you have set ENT_QUOTES
in your invocation of htmlspecialchars.
If you don't want the single-quote to be encoded then you simply need to stop using that flag and switch it for ENT_COMPAT
(which encodes double-quote but not single-quote), so that your statement becomes:
$document->setMetaData('og:title',
htmlspecialchars($document->getTitle(),
ENT_COMPAT, 'UTF-8'));
(I've added the line breaks so it's easy to read on this page, but don't add line breaks in your actual code.)
Also note that htmlspecialchars has a final, optional parameter (added in PHP 5.2.3) called double_encode. It defaults to true
, but if you set this parameter to false
then PHP will not encode existing entities, so even if you did encode the single-quote, the resulting '
would not be further encoded to '
by further invocations of htmlspecialchars, it would be left as '
.
Upvotes: 4