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.

About the Author

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

Alfred - May 28, 2009

I hate editing the code… I always feel like I'm going to break something (and I have…) but this doesn't seem so bad.

Oren Yomtov - May 28, 2009

If you need to delete lots of custom fields after you deactivate a plugin or add lots of custom fields you can use my plugin.

Play Games Win Prize - May 28, 2009

It's amazing how the custom fields could be used. You could basically put a whole script in there.


Sarah - May 28, 2009

Hi Mike, well you were one of the people who said they didn't understand things last time around so I'm hoping that's changed a little now? However, no you couldn't put script in there, just content of some kind, whether that's a word, a sentence, a URL etc. Script wouldn't execute in this manner.

However, there is a lot that custom fields can do, and save time for :) Just think of them as extra text inputs on the page that you can display on the front end.

Indian art - May 31, 2009

that was great info for users like me who are still trying to sort out small technicalities. Coding is not a comfortable thought for most of the users however with posts like these there is bound to be better clarity.

Ernie Smith - June 3, 2009

Hey Sarah,

This is cool and everything, but this is not even close to scratching the surface of what can be done with custom fields. You can do some radical things with it.

For evidence of what can be done with custom fields, I'd like to point you to my site, ShortFormBlog, which uses nothing but custom fields to organize its posts, along with some looped PHP and some if-then-else statements.

By the way, the plugin More Fields is a godsend when working with custom fields, though I had to do some hacking of a plugin to make my RSS feed show the posts.

Any thoughts? Please ask! :)

Phil - June 12, 2009

You have no idea how long ive been looking for those few lines of code, didnt want the over head of a plugin so this is perfect. I stuck a


if ( is_singular() ) {



around the top so that the tags only show up on the singular pages.

Comments are closed