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!

4 comments:

PSD To Wordpress said...

Thanks for this post. Quite informative and useful for me! Hope to see more new posts!

bobble said...

Excellent, hope it helped! I do try to write posts when I can't find instructions elsewhere online, so if I come across any more tweaks and techniques that aren't well covered on other blogs, I'll give them a write-up.

Jeff said...

Hi. I tried this and it worked to display the "else" keywords. However, I'm looking to create a new meta type, and be able to enter the content in the custom field. For example, I'd like to have a new meta tag called <meta name="video_tags" and be able to enter specific video tags for each post. Did I miss a miss a step? Maybe I need to change all the instances of "keyword" in your code to video1, video2, etc?

bobble said...

Hi Jeff,

in theory if you change each instance of 'keyword' (including the variable $keywords etc) to 'video_tags', and then add custom fields called 'video_tags' to your posts, it should work just fine.

Bear in mind that in the code above, keyword1, keyword2, keyword3 and keyword4 are static keywords you can specify in the header code - it is only the $keywords[0] part that pulls anything in from the custom fields, so this part would need to be changed to $video_tags[0].

Once you've done that, assuming you create a video_tags custom field on any given page and put an appropriate video tag into it, the data should be pulled through into your page's header in the same way that the keywords would be.

If you're having problems though, feel free to email me and I'll reply with an adapted bit of code that you should be able to paste into your page header (at your own risk, of course...!).

Post a Comment