My first Joomla experience with meta tags

Posted by Damodar Bashyal on September 29, 2011

 

Our client uses jooblog and there was no option to update meta data for the posts. so i needed to update the meta description, keywords and title. But i had no idea how things work in Joomla.

So after googling for a while and trying few options found in the web, the following worked for me.

File: components\com_jb2\views\post\view.html.php

In the function display add the following code anywhere after $post is set.

$doc =& JFactory::getDocument();
if($doc->getTitle() == 'Technooze Blog')
{
    $doc->setTitle(strip_tags($post->Title) . ' – Technooze Blog - technooze.com');
}
$doc->setDescription( substr(strip_tags($post->Text), 0, 156) );
$doc->setMetaData( 'keywords' ,strip_tags($post->Title) .',' . $doc->getMetaData( 'keywords') );

NOTE:

$doc->setMetaData( 'description', substr($post->Text, 0, 156) );

works too but in my case i can't use because of the hacks, as it would generate meta description twice.

That should have probably worked but i found previous hack at: templates\template_name\index.php

Here meta data was hardcoded so, it was same for all blog pages. so i commented out previous and added mine to catch dyanamic data.

if (strpos ($uri, "/blog/") !== FALSE) {
    $desc = $this->getMetaData( 'description');//"Visit the example.com.au Blog.";
    $keywords = $this->getMetaData( 'keywords');//"blog, example, visit";
}

These variables were used at head (if set): libraries\joomla\document\html\renderer\head.php

I used same rules in: components\com_jb2\views\category\view.html.php for category pages.

$doc =& JFactory::getDocument();
if($doc->getTitle() == 'Technooze Blog')
{
    $doc->setTitle($category->CategoryName . ' – Technooze Blog - technooze.com');
}
$doc->setDescription( 'Read blog posts on ' . $category->CategoryName . ' at Technooze Blog.');
$doc->setMetaData( 'keywords' ,$category->CategoryName .',' . $doc->getMetaData( 'keywords') );

Again, same for: components\com_search\views\search\view.html.php

$doc =& JFactory::getDocument();
if($doc->getTitle() == 'Search')
{
    if(!empty($searchword))
    {
        $doc->setTitle(ucwords(strip_tags($searchword)) . ' – Technooze Search - technooze.com');
        $doc->setDescription( 'Search results for ' . (strip_tags($searchword)) . ' on technooze.com');
        $doc->setMetaData( 'keywords', "search, " . strip_tags($searchword) . ", " . $doc->getMetaData( 'keywords') );
    }
    else
    {
        $doc->setTitle('Search – technooze.com');
        $doc->setDescription( 'Search on technooze.com');
        $doc->setMetaData( 'keywords', "search, " . $doc->getMetaData( 'keywords') );
    }
}

Sketch A Website posted on - Monday 17th of October 2011 03:14:35 AM

don't forget strip_tags and htmlspecialchars where necessary.
 
not published on website


QR Code: My first Joomla experience with meta tags