Dynamic meta tags

With a few lines of code we attempt to dynamically populate our meta tags keywords and description according to the different cases that TXP uses to display our articles:

  • Default metas when TXP shows articles list
  • When visitors are viewing an article category
  • The corresponding metas for an individual article

I chose to use an article to store the default meta keywords and description but you can change this way with a form if you want to protect the information (in this case, only administrator can access to the meta content otherwise in the first case, your client can change its content). I reserve the article ID number 1 for this purpose and to store the keywords into the excerpt (a comma separated list of non textilized words) and the description into the body part (never mind if you use html tag or Textile format: all special tags will be removed).

Create the first article

Create an article as described before, then create a form named meta (type “article”) with these lines in it:

<txp:if_individual_article><txp:php>
global $prefs;
setlocale(LC_ALL, $prefs['locale']);
$keyword = trim(keywords(array()));
$text = preg_replace('/\s+/', ' ', strip_tags(trim(body(array()))));
$explode = explode(' ',$text);
for($i=0; $i<30; $i++) {
	$string .= $explode[$i].' ';
}
$text = strlen($text) < 55 ? title(array()).' '.$text.' '.gTxt('published_at').' '.posted(array('format'=>$prefs['dateformat'])) : trim($string).'&#hellip;';
echo( ($keyword ? '<meta name="keywords" lang="<txp:lang />" content="'.$keyword.'" />'."\r" : '<meta name="keywords" lang="<txp:lang />" content="'.strip_tags(trim(safe_field('Excerpt_html', 'textpattern', 'ID="1"'))).'" />'."\r") );
echo '	<meta name="description" lang="<txp:lang/>" content="'.$text.'" />'."\r";
setlocale(LC_ALL, $locale);</txp:php>
<txp:else />
<txp:if_category>
	<meta name="keywords" lang="<txp:lang/>" content="<txp:php>
echo str_replace('-', ' ', category(array())); </txp:php>" />
	<meta name="description" lang="<txp:lang/>" content="<txp:category title="1" />" />
<txp:else />
	<meta name="keywords" lang="<txp:lang/>" content="<txp:php> $excerpt = safe_field('Excerpt_html', 'textpattern', 'ID="1"'); echo strip_tags($excerpted);</txp:php>" />
	<meta name="description" lang="<txp:lang/>" content="<txp:php> $content = safe_field('Body_html', 'textpattern', 'ID="1"'); echo strip_tags($content); </txp:php>" />
</txp:if_category></txp:if_individual_article>

Call the form in your page template

<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<title>A Textpattern Website</title>
<txp:output_form form="meta" />
(...)

That’s it. You can make some tests now. Please note:

  • the attribute “lang” use the TXP <txp:lang /> tag. You can hard code it.
  • For indivudual articles, the body content is stripped to 30 words followed by an hellips. You can change this word number.
  • If the body content is less than 55 characters, the body content is added followed by “Publish at 14 August 2009 03:36PM” according to your date format stored into your preferences table.

Have fun!

comments powered by Disqus