Elxis CMS Forum

Support => Elxis 4.x/5.x DEV => Topic started by: seadhna on October 13, 2017, 17:28:58

Title: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 13, 2017, 17:28:58
Hi, I am adding the facebook plugin to all articles, which brings in all the required OG META tags for each article very nicely.
But how can I add OG META tags to the Frontpage ONLY?
I tried adding to the index.php of the template, but this then over-writes the OG META for all pages.
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 13, 2017, 18:14:05
I figured this out: components > com_content > controllers > fpage.php

I've added meta tag: $eDoc->addCustom('<meta property="og:image" content="url_of_image.jpg" />');

However, I'm struggling to add the description?

$eDoc->addCustom('<meta property="og:image" content="HOW DO I CALL THE METADESC HERE?" />');
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 13, 2017, 18:14:30
$eDoc->addCustom('<meta property="og:description" content="HOW DO I CALL THE METADESC HERE?" />');
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 13, 2017, 18:28:41
Hi again,
I've figured out the frontpage, but for articles with the FB like plugin, I am adding twitter tags. How do I change this line so it pulls the article description and not the site description?

Many thanks!
            $eDoc->addCustom('<meta property="twitter:description" content="'.$elxis->getConfig('METADESC').'"/>');
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: datahell on October 15, 2017, 10:24:02
The following lines of code can be placed anywhere, eg in your template's index.php file, in order to determine if you are in frontpage. Then you can set your meta tags for frontpage:

$elxuri = eFactory::getURI()->getElxisUri();
$elxis = eFactory::getElxis();
if (($elxuri == '') || ($elxuri == 'content:/') || ($elxuri == '/') || ($elxuri == 'content') || ($elxuri == $elxis->getConfig('DEFAULT_ROUTE'))) {
   //we are in frontpage
}

The proper way to handle article data is to develop a plugin. From the plugin you can access article's meta tags and set your custom meta data.

An alternative way is to use the constant Elxis generates to find the article you are and then query the database to get the information you need.
In article pages Elxis sets the constant ELXIS_ARTID with value of the article's ID.

Here is an example to set article's meta tag for twitter.
if (defined('ELXIS_ARTID')) {
   $db = eFactory::getDB();
   $sql = "SELECT ".$db->quoteId('subtitle')." FROM ".$db->quoteId('#__content')." WHERE ".$db->quoteId('id')." = :xid";
   $stmt = $db->prepareLimit($sql, 0, 1);
   $stmt->bindParam(':xid', ELXIS_ARTID, PDO::PARAM_INT);
   $stmt->execute();
   $metadesc = $stmt->fetchResult();
   if ($metadesc) {
      eFactory::getDocument()->addCustom('<meta property="twitter:description" content="'.$metadesc.'"/>');
   }
}

Code fixed, see below
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 16, 2017, 17:26:25
thanks datahell, which file do I paste this code into?

      if (defined('ELXIS_ARTID')) {
   $db = eFactory::getDB();
   $sql = "SELECT ".$db->quoteId('subtitle')." FROM ".$db->quoteId('#__content')." WHERE ".$db->quoteId('id')." = :xid";
   $stmt = $db->prepareLimit($sql, 0, 1);
   $stmt->bindParam(':xid', ELXIS_ARTID, PDO::PARAM_INT);
   $stmt->execute();
   $metadesc = $stmt->fetchResult();
   if ($metadesc) {
      eFactory::getDocument()->addCustom('<meta property="twitter:description" content="'.$metadesc.'"/>');
   }
}
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: datahell on October 16, 2017, 23:45:28
Anywhere! In order not to modify built-in extensions better place it in your template's index.php file.
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 18, 2017, 13:36:49
Hi datahell,
if I place this in the head of the template's index file, nothing happens, it just appears in the code the same way:

   <script>
       if (defined('ELXIS_ARTID')) {
   $db = eFactory::getDB();
   $sql = "SELECT ".$db->quoteId('subtitle')." FROM ".$db->quoteId('#__content')." WHERE ".$db->quoteId('id')." = :xid";
   $stmt = $db->prepareLimit($sql, 0, 1);
   $stmt->bindParam(':xid', ELXIS_ARTID, PDO::PARAM_INT);
   $stmt->execute();
   $metadesc = $stmt->fetchResult();
   if ($metadesc) {
      eFactory::getDocument()->addCustom('<meta property="twitter:description" content="'.$metadesc.'"/>');
   }
}
   </script>
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 18, 2017, 13:39:12
If I place it in the opening PHP portion, nothing happens:

<?php

defined('_ELXIS_') or die ('Direct access to this location is not allowed');

$eLang = eFactory::getLang();
$eDoc = eFactory::getDocument();
$elxis = eFactory::getElxis();

$touch_icon = $elxis->secureBase().'/images/icons/touch.png';
$eDoc->setMetaTag('viewport', 'width=device-width, initial-scale=1.0');
$eDoc->addLink($touch_icon, '', 'apple-touch-icon');

$eDoc->addJQuery();
$eDoc->addScriptLink($jslink);
unset($touch_icon, $jslink);

elxisloader::loadFile('templates/flex/includes/flex.class.php');
$flex = new templateFlex();

echo $eDoc->getDocType()."\n";

if (defined('ELXIS_ARTID')) {
   $db = eFactory::getDB();
   $sql = "SELECT ".$db->quoteId('subtitle')." FROM ".$db->quoteId('#__content')." WHERE ".$db->quoteId('id')." = :xid";
   $stmt = $db->prepareLimit($sql, 0, 1);
   $stmt->bindParam(':xid', ELXIS_ARTID, PDO::PARAM_INT);
   $stmt->execute();
   $metadesc = $stmt->fetchResult();
   if ($metadesc) {
      eFactory::getDocument()->addCustom('<meta property="twitter:description" content="'.$metadesc.'"/>');
   }
}
?>
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: datahell on October 19, 2017, 23:01:25
I have a small error in my code. Here I write the fixed one. Tested and works. It should work for you too.

if (defined('ELXIS_ARTID')) {
   $db = eFactory::getDB();
   $x = ELXIS_ARTID;
   $sql = "SELECT ".$db->quoteId('subtitle')." FROM ".$db->quoteId('#__content')." WHERE ".$db->quoteId('id')." = :xid";
   $stmt = $db->prepareLimit($sql, 0, 1);
   $stmt->bindParam(':xid', $x, PDO::PARAM_INT);
   $stmt->execute();
   $metadesc = $stmt->fetchResult();
   if ($metadesc) {
      eFactory::getDocument()->addCustom('<meta property="twitter:description" content="'.$metadesc.'"/>');
   }
}
Title: Re: Is it possible to add meta tags to Frontpage ONLY?
Post by: seadhna on October 25, 2017, 13:37:39
Thanks! This works. That's great. Is it possible though to have the description match the language? With this solution, the meta twitter description is always in English.