Put your CMSMS Metatags on autopilot

What i love about CMSMS and Smarty Templating engine is that many issues can be solved with very little effort.
As we all know maintining a Website can sometimes become fairly painfull and timeconsuming and this can also involve setting and mainting Meta tags.

Some of you probably know there is SEOTools module or Autometa plugin that can help in these terms, but as i am not a big fan of using modules when there is no real need for it i want to show you how easy it is to setup atuomated Meta tags with nothing but Smarty and our content.

First thing we need to do is open up our Page template and add assign our content and title tag as we want to use it Multiple times in our Template.
Right after {process_pagedata} add following.
{strip} {* use strip to get rid of whitespace *}
{content assign="get_content"} {if !isset($get_title)} {title assign='get_title'} {/if} {/strip}

So in this step we assigned our {content} tag and captured {title} tag. If statement is used to replace Page title with titles from News other module, you can find Detailed description about this part in Wayne's post "Setting your page's title to be the News title" but we will get back to this later on.

As we have assigned title and content you should remeber that we have two variables instead of regular tags, this also means that you should replace {content} tag in your Template with {$get_content} and {$get_title} for {title}.

Now as <title> comes first we will edit this part.

<title>
  {$get_title} - {sitename}
</title>

As we have our Title set we can move to the rest of our Meta tag structure. 

<meta name="title" content="{$get_title} - {sitename}" />
<meta name="copyright" content="{sitename}" />
<meta name="description" content="{$get_content|strip_tags|truncate:'150'|strip}" />
<meta name="keywords" content="{search action='keywords' count='15'}" />
<meta name="language" content="{$lang}" />
<meta name="url" content="{root_url}" />
<meta name="identifier-URL" content="{root_url}" />
<meta name="revised" content="{modified_date format="%a, %d %b %Y %H:%M:%S"}" />
<meta name="original-source" content="{if isset($canonical)}{$canonical}{elseif isset($content_obj)}{$content_obj->GetURL()}{/if}" />

So what did we do so far?

As you can see each of our Meta tags is populated with Smarty tags, meaning so far our content is populated automaticaly, means simply "Set it and forget it".

We have used our {$get_title} variable for "title" tag, then as we have {sitename} available we can use it for "copyright" tag, but you could also use a GCB for this part if sitename isn't identical to company name.
Our {$get_content} is used to populate the "description" tag, we also used strip_tags modifier to remove html tags from content and truncate to limit number of character.
The "keywords" tag is simply using {search} Module tag, but it is advisable that you check this part from time to time and exclude unneeded keywords in Search Module options.
For "url" and "identifier-URL" tags we simply use {root_url} and for "revised" tag we made use of {ldelim}modified_date{rdelim} Plugin that comes with CMSMS core, this way when a page is updated this part is updated to.
In the "original-source" tag we use a part of code that is already included in Default CMSMS Templates for <link rel="cannonical" href="#" /> tag.

I usually place above code simply in Template, as it is not like i would need to update it regulary, but it would also work from "Global Settings -> Global Metadata" of CMSMS System if you prefer it.

What about Modules?

As i mentioned before, we will look at News Module as a sample of how you could populate above tags with other Modules.
What we need now is open up News Detail Template in "Content -> News -> Detail Templates" and edit it by adding following code on top of our News Template.

{if isset($entry->canonical)} 
    {assign var='canonical' value=$entry->canonical}
{/if}
{assign var='get_title' value=$entry->title}
{if $entry->summary}
    {assign var='meta_descr' value=$entry->summary|truncate:'150'|strip_tags|strip}
{else}
    {assign var='meta_descr' value=$entry->content|truncate:'150'|strip_tags|strip}
{/if}
{if $entry->postdate}
    {assign var='meta_date' value=$entry->postdate|date_format:'%a, %d %b %Y %H:%M:%S %z'}
{/if}

You should be familiar with first part, at least cannonical part is included in Default templates, then we have our $entry->title assigned as {$get_title}, now you know what we did in the steps described before and next we have assign summary or content part of News to a {$meta_descr} variable and as last we have assigned postdate to {$meta_date} variable.

As we have our variables set, "title" tag should already work with {$get_title} variable and now all we need to do is change few lines of our Meta tag part we did before.

First we edit "description" tag, as we have {$meta_descr} variable available if viewing detail page of News post, we can now check for $meta_descr, if variable is available our page content is replaced with news content.

<meta name="description" content="{if isset($meta_descr)}{$meta_descr}{else}{$get_content|strip_tags|truncate:'150'|strip}{/if}" />

Next would be "revisied" tag, as we have assigned our postdate to {$meta_date} we can now use it for this part.

<meta name="revised" content="{if isset($meta_date)}{$meta_date}{else}{modified_date format="%a, %d %b %Y %H:%M:%S"}{/if}" />

Well thats it, we now have fully automated Meta tags for our content and news pages. Note that you are not only limited to my examples above but, you can use same methods for Open Graph Protocol tags or new IE9 feature "Pinned Site", for example you could even add latest News posts to msapplication-task tag or Shortcuts to your Contact page or whatever you prefer, where user could quickly access these links from Windows 7 taskbar.

Hope i was able to give you some ideas and tipps in this post and have fun trying it.

Comments