Textpattern tips, tutorials and code snippets

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:

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 />
	<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>" />

Call the form in your page template

<!DOCTYPE html>
<html dir="ltr" lang="en">
<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!

4 Comments Comment feed

You should list some related articles as there are easier methods to accomplish dynamic meta tags using standard TXP tags and article fields.

I agree with Markus about presenting a few easier methods for managing meta tags (like using rah_metas), as this one could look a little scary for TXP newcomers.

No doubt this trick by Pat64 adds a few nifty tricks…

I think it would be helpful to list some of the other methods for those reading this tip.

Even if this or the other tip is not considered to be the optimum method, what is interesting is to present different means to obtain a goal – one of the great assets of Textpattern is its flexibility…

Hi Folks

sure Jukka’s rah_metas plugin is an alternative (perhaps the only one) but I’m not sure it supports categories. I’d post this tip because I needed a solution for this context.
Submit yours as Jonathan said above: I am personaly interested by a better approch.
Best regards,

Add a comment

Use Textile help to style your comments