Add meta tags to WordPress with custom fields

Ready for a bit of WordPress wizardry? OK, here goes...

There are plenty of SEO plugins out there to add meta tags to individual posts and pages on WordPress. But you don't really need them - you can add custom meta keywords and description tags to a WordPress template using custom fields, instead.

NB This method requires you to have access to edit your template code - ie, it will work on websites that use WordPress as their CMS, but not on free WordPress-hosted blogs at wordpress.com.

The Template Code

First things first, you need to give your template the PHP code in its header template to allow it to detect whether a custom field has been created for the keywords or description meta tag (or both).

In WordPress, click Appearance, and then Editor from the left-hand menu. Then click on your header file at the right - usually titled 'Header' and named 'header.php'.

Below the <head> tag, add the following section of code:

<meta name="keywords" content="<?php
while (have_posts()) : the_post();
$keywords = get_post_meta($post->ID, 'keywords', false);
if ($keywords){
echo 'keyword1, keyword2, '.$keywords[0].', keyword3, keyword4';
}
else
{
echo 'keyword1, keyword2, keyword3, keyword4';
}
endwhile;
?>" />
<meta name="description" content="<?php
while (have_posts()) : the_post();
$description = get_post_meta($post->ID, 'description', false);
if ($description){
echo $description[0];
}
else
{
echo 'Generic description here.';
}
endwhile;
?>" />

What does it do?

The functionality is very simple. Each time your template loads a page, it follows this process:

  1. begins the meta keywords tag

  2. checks for a custom field named 'keywords'

  3. if one is found, injects a list of keywords containing the custom text

  4. if none is found, injects a standard list of keywords

  5. closes the meta keywords tag

  6. begins the meta description tag

  7. checks for a custom field named 'description'

  8. if one is found, injects the custom text

  9. if none is found, injects a standard site-wide description

  10. closes the meta description tag
You will need to edit the above code to put your own default keywords and description in place.

Simply modify the 'keyword1, keyword2...' and 'Generic description here.' sequences to match your own preferred meta information.

Note also that, even if a keywords field is found, there is the option to add generic keywords to the front and/or end of the custom text - this is so any site-wide keywords can be included without you having to type them over and over in your pages' custom fields.

How to Use

While editing an individual page or post, look for the Custom Fields area, below the main editing panel.

If you cannot see Custom Fields, click on Screen Options at the top-right, then tick the box alongside Custom Fields.

The first page you edit, click 'Enter new' in the Add New Custom Field box.

Call your custom field keywords and enter the list of keywords you want, separated by commas, in the Value box.

Click 'Add Custom Field'.

Repeat, this time calling your custom field description and entering a full-sentence description of the page you are editing.

Don't forget to click the Update button, top-right, to save the changes to your page's Custom Fields information.

For subsequent pages, you should be able to select the necessary Custom Field name - keywords or description - from the dropdown list, reducing the risk of a typo!

And that, really, should be it. Just try to follow these rules:

  • get your PHP in the right place - between the <head> tags and don't paste it in the middle of another section of PHP!

  • don't forget to edit the code to include your own preferred default keywords and description - and those site-wide keywords to accompany your custom ones, if you want to use them

  • remember to look to Screen Options if you can't see Custom Fields by default, on both pages and posts

  • spell 'keywords' and 'description' right the first time you create them - the code demands it!
I welcome questions/comments in the comments section below, or via Twitter - but be warned, although I did create this method from scratch, I'm no PHP expert and will not be held responsible if you break your template while making manual edits!