graphicpush

Thoughts on branding, design, writing and life by Kevin Potts. Established 2003.

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:

<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.

, , , ,

commentary + criticism

Lee

wrote the following on Saturday May 14, 2005

Thanks for the write up, very helpful.

Kevin

wrote the following on Monday May 16, 2005

No problem. Hope it helps someone out there.

mickael

wrote the following on Wednesday May 18, 2005

this is excellent, thank you

Bob

wrote the following on Tuesday May 24, 2005

I’ve tried this for a site I’m building, and while the meta information shows up just as it should, the archive page displays the comment form at the very top of the page.

I think it’s the first time I’ve seen anything in the display on the page like this, and I can’t figure out how to prevent it.

Darla

wrote the following on Sunday June 19, 2005

Strange, I thought I’d posted here before?

Anyway, Bob, I’m having the same issue as you are. I end up with something like a “double header” with the page turned upside down.

Kevin responded and asked to see my code … so hopefully, together we can figure this out. I’d LOVE to be able to have custom metatags on all my posts!

Take care.

Jay

wrote the following on Saturday July 9, 2005

Very helpful, do you know is there anyway to make the custom1 field bigger simlar to keywords or excerpts?

Keep up the great work.

Kevin

wrote the following on Saturday July 9, 2005

Unfortunately, no. Since I am not a PHP guru it is tough for me to decipher the code, and I have not figured out how to change the field to a text area.

Hao Xuan

wrote the following on Saturday February 11, 2006

Vietnam housing: room, apartment, car…

Vietnam Property

wrote the following on Sunday February 26, 2006

Vietnam Property Finder: vietnam apartment, vietnam house, vietnam room, vietnam car…

Lozz

wrote the following on Sunday April 30, 2006

I have the same problem as Bob where it puts the comments fields at the top of the archive page.
I’ve also found that it puts the meta tag lines for all of the articles into the main page.

Any suggestions?

GrumpyOldMan

wrote the following on Sunday June 4, 2006

To prevent multiple Meta-Tags according to Step4, you have to limit the number of articles, like:

So on any archive-page or every other template, there will be always used the latest article for meta retrieval.

gosh – it took me half an hour to understand it myself – and no other resource about this found on the net.

GrumpyOldMan

wrote the following on Sunday June 4, 2006

Uh, sorry – this blog strips the code.

You have to add … limit=”1” within the article-call.

matthew

wrote the following on Friday September 15, 2006

question: will txp only write a custom field if it’s the same form that generates the articles?

for example: my page calls a form for my html head, and then another form to write the article. can a meta data custom field be included in the head form?

i can’t get it to work.

thanks for this write up.

Eros Kasikantiris

wrote the following on Thursday November 2, 2006

Hi
This is my first time using Filezilla. My webmaster set it up for me but did not show me how to use it. I read through their instructions but could not find where I actually get into the the web page I want to work on.
The files I want to work on are downloaded on Filezilla but when I click on the file I want to work on, a text Box opens and ask if I want to Replace the existing file with an older one. The point is, I never seen the file (web page) open for me to make any changes in the first place.
What do I do?

Thank you very much
Eros

tom

wrote the following on Monday July 2, 2007

I am using this method to have meta tags on my static article (sticky) pages.
I use the following code:
txp:article form=“meta_tags” limit=”1” status=“sticky”/>”

(cant work out how to post tags here)

This works fine, however when sticky article is opened up from the search results listings I get the comments box in the head as well adding limit=”1” makes no difference. Does any one have any advice

Manuel

wrote the following on Friday July 20, 2007

for those with the repeating comments block on the head, this tutorial has an error, is using txp:article form=
when it should use txp:output_form form=“meta” />
That will correct the error

Kevin

wrote the following on Friday July 20, 2007

This is an old article written for Textpattern many versions ago. For the most up to date methodology for creating dynamic meta, I very much recommend picking up Textpattern Solutions, which dives deeply into the process.