How to add different OG and twitter meta tags for each article in Joomla

370 views Asked by At

How can I use OG and Twitter meta tags in Joomla pages. Is there any plugin which creates additional fields in the article editor under 'publishing'tab ? Thanks in advance.

1

There are 1 answers

0
Luis H Cabrejo On

From what I have investigated, Facebook does automatically place all description, title and picture in its place. Twitter does not. You need to added manually. I had the same problem fixed with this code. I was trying to get Twitter to add my link just as facebook does automatically but had to investigate the CARD Validator here https://cards-dev.twitter.com/validator ... The way I have it working on Joomla 3.9.11 is adding this code in the head of my template default.php ... I was able to properly add the title, the description and the right picture, usually the first one on the article.

<head>
<?php 
//Added for TWITTER
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);
    $theArticle = $article->get("title");
    $theImages = $article->get("images");   
    $pictures = json_decode($theImages); // Split the parameters apart
    $timage= "http://yourdomain.com/".$pictures->{'image_intro'};
}
$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="twitter:title" content="'.mb_strimwidth(strip_tags($theArticle),0,225, " ...").'"=""/>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="yoursite">
<meta name="twitter:creator" content="yourcreator">
<meta name="twitter:url" content="'.JURI::current().'">
<meta name="twitter:description" content="'.mb_strimwidth(strip_tags($doc->getMetaData( 'description' )),0,225, " ...").'"=""/>
<meta name="twitter:image" content="'.$timage.'">
');
?>
<!-- This is what is already on Joomla Headers -->
    <jdoc:include type="head" />
    <?php $this->loadBlock('head') ?>
</head>