Elxis CMS Forum

Support => General => Topic started by: jorgebravoc on March 30, 2011, 08:06:13

Title: Meta Description Enhanced
Post by: jorgebravoc on March 30, 2011, 08:06:13
Hello pals:

Working with Elxis is so great, I find it very useful and flexible. Once I came across an issue about the meta Description tag..... I noticed that it grabs certain "keywords" or some random words from the text..... when sharing a content item through Facebook the Meta Description tag is fundamental as it will show up under the title of the page.... so I wanted to have the first part of every article show up as the Description in order to get a "text preview" of the shared item...

A solution I found was to alter the code in the Components/com_content/content.php file  and make the Description tag be the actual Introtext... ($row->introtext) but it has to be truncated as a description shouldn´t be too large. So a truncating functino must be added and the limit number of characters should be indicated... Here is the code you need to replace if you want your Description Meta Tag to be the first chunk of your text,


Before you make the following changes you must:

- Backup your content.php file
- Look at the source code of your website and identify the Meta Description Tag at the top in order to compare it after making the changes and refreshing your page

Components/com_content/content.php    Line 1495 Replace    with this

Code: [Select]
/********** START META DATA MANIPULATON *********/
    if ($makeMeta) {

function myTruncate2($string, $limit, $break=" ", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;

$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}

$description = $row->introtext;

$shortdesc = myTruncate2($description, 600);


        $metaKeys = eUTF::utf8_trim($row->metakey);
        if ($metaKeys == '') {
            $keys = array();

            $keytitle = $row->title;
            if ($row->categorytxt) { $keytitle .= ' '.$row->categorytxt; }
            if ($row->sectiontxt) { $keytitle .= ' '.$row->sectiontxt; }
            if ($row->author) { $keytitle .= ' '.$row->author; }
            if ($row->language != '') { $keytitle .= ' '.str_replace(',', ' ',$row->language); }
            $keytitle = preg_replace('/(\!|\@|\$|\%|\&|\(|\)|\,|\;|\'|\"|\.|\#|\?|\[|\]|\`|\*)/', '', $keytitle);

            $parts = preg_split('/[\s]/', $keytitle);
            if (count($parts) > 0) {
    foreach ($parts as $part) {
    if (eUTF::utf8_strlen($part) > 3) { $keys[] = $part; }
}
}

            $keys = array_unique($keys);
            if (count($keys) > 20) {
                $keys = array_slice($keys, 0, 20);
            } else if (count($keys) < 5) {
                $keys[] = _E_CATEGORY;
                $keys[] = _E_CONTENT;
                $keys[] = _E_SECTION;
            }
            asort($keys);
            $metaKeys = implode(', ',$keys);
        }

$ttl = ($row->categorytxt) ? $row->title.' - '.$row->categorytxt : $row->title;
$ttl = preg_replace('/(\@|\$|\%|\&|\'|\"|\#|\[|\]|\`|\*)/', '', $ttl);
        $mainframe->setPageTitle($ttl);
        $mainframe->addMetaTag('author' , $row->author);
        $mainframe->setMetaTag('description', $shortdesc);
        $mainframe->setMetaTag('keywords', $metaKeys);
    }

    /********** END META DATA MANIPULATON *********/

Hope it helps all of those that want to have a nice description tag not only random words of a single sentence. This is not by all means necessary to be implemented.... if you want to do it here is how... if you rather stick to the way Elxis creates your Meta Description Tag then go  ahead, Elxis handles it pretty good anyways.

Have a nice day and let me know if this creates any sort of bugs... I tested it and it works fine....
Title: Re: Meta Description Enhanced
Post by: datahell on March 30, 2011, 09:09:33
Dont use strlen, strpos and substr as those functions do not support utf-8 characters, use eUTF functions instead.
You can do the same thing in one line:

$metadesc = (eUTF::utf8_strlen($string) > $limit) ? eUTF::utf8_substr($string, 0, $limit - 3).'...' : $string;

Note that elxis reads meta data from the article parameters. So you can set your meta description to exactly what you want from the elxis admin area.