Custom Meta Tags in Textpattern
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:
<pre>
'custom_1_set' => 'custom1',
'custom_2_set' => 'custom2',
'custom_3_set' => '',
'custom_4_set' => '',
...
</pre>
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:
<pre>
'custom_1_set' => 'meta_description',
'custom_2_set' => 'meta_keywords',
</pre>
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:
<pre>
<meta name="description" content="<txp:custom_field name="meta_description" />" />
<meta name="keywords" content="<txp:custom_field name="meta_keywords" />" />
</pre>
The <txp:custom_field /> tag was added in RC3 v309 and allows the output of the custom tags we renamed in Step 1. The rest is just the standard meta calls placed in the head of an HTML document.
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:
<pre>
<txp:article form="meta" />
</pre>
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:
<pre>
<title>Your Site Name: Article Title</title>
<meta name="description" content="your description" />
<meta name="keywords" content="your, keywords, here" />
</pre>
The More Meta the Better
Google, Yahoo and MSN love meta information, and the more you have, the better off your page will be in terms of semantics and accessibility. Besides description and keywords, you could also add author information plus additional navigation aids. I have one master meta form that contains all my head information, from the title down to the CSS and JavaScript links. I use the one call described in Step 4 across every template (think of it as an include) so I only need to edit the one form to make a change site-wide.
Comments.
Lee
- wrote the following on Saturday May 14, 2005
Kevin
- wrote the following on Monday May 16, 2005
mickael
- wrote the following on Wednesday May 18, 2005
Bob
- wrote the following on Tuesday May 24, 2005
Darla
- wrote the following on Sunday June 19, 2005
Jay
- wrote the following on Saturday July 9, 2005
Kevin
- wrote the following on Saturday July 9, 2005
Hao Xuan
- wrote the following on Saturday February 11, 2006
Vietnam Property
- wrote the following on Sunday February 26, 2006
Lozz
- wrote the following on Sunday April 30, 2006
GrumpyOldMan
- wrote the following on Sunday June 4, 2006
GrumpyOldMan
- wrote the following on Sunday June 4, 2006
matthew
- wrote the following on Friday September 15, 2006
Eros Kasikantiris
- wrote the following on Thursday November 2, 2006
tom
- wrote the following on Monday July 2, 2007
Manuel
- wrote the following on Friday July 20, 2007
Kevin
- wrote the following on Friday July 20, 2007
