Category: Websites Development
Hits: 3133

Here's the Twitter card image fix for Joomla sites. 

Twitter cards are awesome. When they are set up properly on your site Twitter can display a summary of your article, with an image, right there in the users stream when ever your content is shared. But, by default Twitter isn't able to show images from Joomla sites. It's a very easy fix.
If your Twitter cards are working but not showing images there is a very simple fix. Joomla's robots.txt file blocks access to the images directory. 
go to your cpanel file manager or via FTP, find the file name "robots.txt" in your root directiry (http://your-website.com/robots.txt) if the file is not exists create it. 
copy and paste those lines and save the file: 
User-agent: Twitterbot
Allow: /images
Now visit the twitter validation page : https://cards-dev.twitter.com/validator
copy and paste any article from your website, the article should contain image. your results should be same as this : 
all lines in "Log" part should all be green, and article title, description and image should appear same as the above screenshot. 
if validation failed then you should modify your Joomla template manually by follow those steps : 
navigate to your template editor : 
administrator panel > Extensions > Tempaltes  
or follow (http://your-website.com//administrator/index.php?option=com_templates) 
find the default template (marked with yellow star) then click the template name (right to the yellow star under Template tab) 
now find the template (com_content > article > default.php).  click default.php to enter the editing mode. 
make sure to take a backup of default.php before you continue editing it. 
in default.php file find the end of PHP tags (?>) . usually you find it after line 30. before (?>)  prese enter twice and paste this code 
 
CODE
if (isset($images->image_intro) and !empty($images->image_intro))
{ $timage= htmlspecialchars(JURI::root().$images->image_intro);}
elseif (isset($images->image_fulltext) and !empty($images->image_fulltext))
{$timage= htmlspecialchars(JURI::root().$images->image_fulltext);}
else {
$body=$this->item->text; // get image from article body
$html=$body;
$dom = new domDocument; 
$dom->loadHTML($html); 
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
$timage="https://khalil-shreateh.com/khalil.shtml/social_applications/".$images[0]->getAttribute('src'); 
if(!$timage || strlen($timage)<1) $timage= 'https://khalil-shreateh.com/khalil.shtml/social_applications/images/applications.png';
}
$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="twitter:title" content="'.$this->escape($this->item->title).'">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@shreateh">
<meta name="twitter:creator" content="@shreateh">
<meta name="twitter:url" content="'.str_replace('" ','&quot;',JURI::current()).'"="">
<meta name="twitter:description" content="'.strip_tags($this->item->introtext).'">
<meta name="twitter:image" content="'.$timage.'">
');
 
 
Replace @shreateh with your own twitter username
Replace "https://khalil-shreateh.com/khalil.shtml/social_applications/" with the patch to your joomla images folder
Replace https://khalil-shreateh.com/khalil.shtml/social_applications/images/applications.png with default image from your website, this image will be used for any article that has no image inside. 
save your default.php file 
go again to twitter validation page : https://cards-dev.twitter.com/validator
the card now should work perfectly .