Revisiting WordPress Custom Fields

A few months ago I wrote about Custom Fields in WordPress. At the time I just gave a small example of how these could be used, and a couple of people still didn’t grasp the point of them so I’m going to give a few more examples of using them which will hopefully shed some light on how useful they actually are!

For the basics of using the custom fields in the admin then please read the custom fields post first. Below are just working examples.

Meta Description

Before you mention (or think it), yes there’s the fantastic All in One SEO pack, but I don’t use this for my blog as I don’t need that much extra code running. I don’t think that this plugin is needed for blog posts however, sometimes a meta description is. So instead of adding a full blown plugin for the benefit of a few pages, we can just use a custom field.

So if you set up a custom field for each post that you want to add a meta description for, give the custom field a name of metad and the value should contain the description. Don’t forget, once you create the custom field the name will be available automatically for all your other posts and pages.

Then in your header.php file you want to add the meta description tag in. You can either just use

[php htmlscript=”true”]<meta name=”description” value=”<?php echo get_post_meta($post->ID, ‘metad’, TRUE); ?>” />[/php]

Or, if you don’t want an empty description tag if there’s no description then you can use an if statement with it ie.

[php htmlscript=”true”]<?php $metad = get_post_meta($post->ID, ‘metad’, TRUE);
if ($metad) : ?>
<meta name=”description” value=”<?php echo $metad ?>” />
<?php endif; ?>[/php]

This will only put a description then if one exists for the post or page currently being viewed.

Post Image

A lot of bloggers like to add an image to their posts, and whilst this is easily done in the post content area, you could reduce your work by using a custom field. If you create a custom field for a post with a name of postimage and then the value is the URL of the image (you can still upload the image via the image uploader, but instead of inserting it into the post, just copy the URL). We also need some alternative text so create a second field called postimgalt and enter the text for the alt attribute in the value field.

Then in your single.php page (and index.php, archive.php and category.php pages if each exists and you’re showing full content posts in there) add the image code where you want the image to be placed. For example if you wanted the image to be at the top right of your post then you could use the following just before the template tag the_content();

[php htmlscript=”true”]<?php $postimage = get_post_meta($post->ID, ‘postimage’, TRUE);
if ($postimage) : ?>
<img src=”<?php echo $metad ?>” alt=”<?php echo get_post_meta($post->ID, ‘postimgalt’, TRUE); ?>” class=”imgright” />
<?php endif; ?>[/php]

Note the class imgright would then have the styles:

[css].imgright {
float: right;
margin: 0 0 10px 10px;

This way you would then just have content in your post and your post image would go into the custom fields.

Specify the Language

Last year we created a multilingual site in WordPress for a client, yet still wanted to be able to add the lang and xml:lang attributes to the HTML tag, so we created a custom field called langtype and then used this to insert the correct type ie.

[php htmlscript=”true”]<?php
$langtype = get_post_meta($post->ID, ‘langtype’, TRUE);
if (empty($langtype)) $langtype = ‘en’;
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “”>
<html xmlns=”” xml:lang=”<?php echo $langtype ?>” lang=”<?php echo $langtype ?>”>

This is useful for sites running two or more languages.

Credit Author

Sometimes you may have a guest author write for your site. Yes, you can create them an account for this but then that means you need to leave the account there for the lifetime of the blog (on account deletion, the author’s post will be recredited to the site admin or whoever you select during account deletion). Instead of creating an account for each guest author you have, especially if it’s only a one off, you can create a custom field to credit the author. So for a guest post, create a field called pauthor, and enter the Author’s name. If you want to link their name then you can also create a second field called pauthurl and enter the author’s website (note I’m just making up fieldnames here. Just make sure they’re unique and unlikely to clash with anything else already in the database table).

Then in your template files, where it usually says the author’s name, we can replace this with displaying the guest author, if one exists, else display the post author eg.

[php htmlscript=”true”]Post written by:
$pauthor = get_post_meta ($post->ID, ‘pauthor’, TRUE);
if ($pauthor) :
echo “<a href='”.get_post_meta ($post->ID, ‘pauthurl’, TRUE).'”>”.$pauthor.”</a>”;
else :

This would replace your usual “Post written by: Sarah” code. Of course each theme is different so you will have to determine what to replace where.

Alternatively you could just add a line at the top of the post if there is a guest author set eg.

[php htmlscript=”true”]<?php
$pauthor = get_post_meta ($post->ID, ‘pauthor’, TRUE);
if ($pauthor) :
echo “<p>The following post is a guest post by <a href='”.get_post_meta ($post->ID, ‘pauthurl’, TRUE).'”>”.$pauthor.”</a>.</p>”;


Hopefully this has given a few more examples of custom field usage to help you understand how you can use them, or for those who haven’t seen them before, given you some new ideas on how you can add unique content to your posts and pages without too much effort.

Disclosure: In full disclosure, it is safe to assume that the site owner is benefiting financially or otherwise from everything you click on, read, or look at while on my website. This is not to say that is the case with all content, as all publications on the site are original and written to provide value and references to our audience.


  1. Alfred May 28, 2009
  2. Oren Yomtov May 28, 2009
  3. Play Games Win Prize May 28, 2009
  4. Sarah May 28, 2009
  5. Indian art May 31, 2009
  6. Ernie Smith June 3, 2009
  7. Phil June 12, 2009