Custom Meta Tags in Textpattern
Textpattern does not offer an obvious way to add meta tags to a template, but its easy to implement a custom description and set of keywords for each article.
This is just a quick tip, but I thought I’d share it since I’ve seen it come up a few times on the forums. Textpattern has a tendency to render a lot of non-content related source code behind the scenes, which is fine in most cases, but there are times when getting under the hood is important.
Writing custom meta tags for each article is extremely important for search engine optimization, but Textpattern does not offer an easy “meta tag” that can be thrown into a template.
If you are using RC1, the hard work has already been done. Joel Dueck has written a plug-in that uses the “custom1” and “custom2” fields under an article’s Advanced Options tab to produce nice meta. You can get grab the _jad_metadata_ plugin along with well-written instructions at Textpattern Resources.
Textpattern RC3 is being constantly updated by Dean and company, and as of revision 309, TXP allows you to directly output the contents of the custom fields mentioned above. To use them for meta, follow these easy steps:
Step 1: Edit the Custom Field Names
Make sure you have the latest build of RC3. Open your favorite FTP editor (I like FileZilla) and navigate to your Textpattern directory. Open lib/admin_config.php and scroll down to the bottom until you see an array that looks like this:
'custom_1_set' => 'custom1',
'custom_2_set' => 'custom2',
'custom_3_set' => '',
'custom_4_set' => '',
These fields exist for your editing pleasure. Custom1 and custom2 have been set up already but are not being used, and you can change the name to whatever you want. Similarly, TXP allows up to ten custom fields, and to make them active, simply give them a name between the quotes in the second column. For the time being, change the first two lines to the following:
'custom_1_set' => 'meta_description',
'custom_2_set' => 'meta_keywords',
Do not change the first column names. Save admin_config.php and overwrite the old version.
Step 2: Add Your Meta
From the Textpattern CMS, open an article in Contents/Articles. Click the “Advanced Options” link in the left column, and below the “Override Form” field, you’ll see your custom fields. Add some appropriate content that is relevant to that article.
Below your fields “meta_description” and “meta_keywords,” you will see another text area marked “Keywords.” Do not use the “Keywords” text area for adding your keywords. This is a proprietary variable used by Textpattern for its search engine and for finding related articles, and the content will not show up in your HTML meta tag.
Step 3: Create the Form
Inside Textpattern, go to Presentation/Forms. Click “Create a new form” and copy the following into the text area:
<meta name="description" content="<txp:custom_field name="meta_description" />" />
<meta name="keywords" content="<txp:custom_field name="meta_keywords" />" />
Call the form “meta” and set the Form Type to “article.” Save the form.
Step 4: Editing Your Template
From Presentation/Pages, open whatever page you use as the article template; if you’re using the default TXP install, open “archive.” In the head of the document, you should see the title tag. Below that, add the following:
<txp:article form="meta" />
This will output the data of the form we just created, relative to whatever article the template is displaying. (Make sure to not use <txp:output_form /> since that is used for generic, non-article calls.) Save the page template, direct your browser to your site and open the page containing the article you edited in Step 2. If you view the code, you should see the following at the top of the document:
<title>Your Site Name: Article Title</title>
<meta name="description" content="your description" />
<meta name="keywords" content="your, keywords, here" />
The More Meta the Better