PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

WordPress Custom Fields

Posted by on 2nd Nov 2008 WordPress Coding & Design 8 comments

For many WordPress users the custom fields box is an unused section on the Write Post/Page page. What exactly are custom fields? Custom fields allow you to assign additional information to that particular post (or page, but from here on out I’ll just call it a post), and only that post ie. the information you add will only be available to that page.

For example, maybe you want to include a unique comment for every post you write, but display this in the sidebar, or have it go in your header.

Set the Custom Field

First off, you open up the Write Post page and write your post as normal. Then scroll down to the Custom Fields section and open it up. Now we need to select your key. Check the drop down list to see which keys already exist. Let’s call this key ‘quote’. If it’s not in the select list then type it in the text input box to the right of the select list (once you’ve typed it in once it will appear in the select list as an available key for all posts and pages on your site). Then in the textarea you can type the value for this key which would be a quote from your post, for example for this post I could use

For many WordPress users the custom fields box is an unused section

Once you’ve added your quote click the Add Custom Field button to save it. Then Save or Publish the post. At the moment the quote won’t be seen, so now we need to edit the template to show it.

Display a Custom Field

We’re adding this quote to the header, however you could add it in any template file. For this, we would open up the header.php theme file and decide where to add the quote. To retrieve the value of the quote we simply need to use

<?php echo get_post_meta($post->ID, 'quote', TRUE); ?>

Here the template tag we’re using is get_post_meta() which can take 3 parameters. The first is the ID of the post, the second is the name of our key and the third controls whether we’re looking for a single value for this key or multiple values for this key. In this instance we’ll only have one quote per page so this is set to TRUE (meaning single, FALSE or omitted would make it search for all values of the key ‘quote’ assigned to the post).

This PHP code can of course be put in between blockquote tags, or any HTML tags.

Custom Field Uses

A number of popular plugins use custom fields to do their work, however you don’t realise this as the plugin authors have essentially duplicated the custom field input boxes and given them names, for example the All in One SEO pack uses custom fields for the page title, meta description and meta keywords. Of course this offers a number of other options too, and as always, plugins take the hard work out of editing your theme, but you could just use the custom fields section to add a meta description and use the PHP code above to print the description for that post or Page into the meta description tag.

Other examples of use could be

  • To select a specific image for your header
  • To add in just a meta description for your post without the need for a plugin
  • To add in the language type of the post (if you wrote in more than one language) and have this output into the language attributes of the HTML tag
  • To create a unique piece of information for your sidebar
  • To add in details for a sponsor for that particular post

There are many options for this under-used feature :)

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.

8 comments - Leave a reply
  • Posted by Mario Andrade on 2nd Nov 2008

    Nice tutorial, do you know how to use custom fields to allow any user write content?

  • Posted by Clare on 2nd Nov 2008

    I think this is probably one of the most under-used functions of wordpress. I’ve had a few themes in the past that have made good use of it, usually magazine style themes.

  • Posted by Mike Huang on 2nd Nov 2008

    I had no clue what this was ever suppose to be used for and I still don't know :(

    -Mike

  • Posted by Sarah on 2nd Nov 2008

    Mario, I’ve never looked into that no. I assume you mean any visitor to your site? or do you mean any user with a login account on your site?

  • Posted by Sarah on 2nd Nov 2008

    Mike, glad to know my post cleared it all up for you :???:

    Simply put, a custom field is a bit like having a second (and third if you wish) content box for that post. One of my examples of use was a meta description. Forget your SEO plugins for a moment because I don't think you need them for blog posts.

    When you're creating a post you can set the post title and the post content. So if you created a custom field for the meta description, then in your template you could have the meta description field in the header.php file retrieving this meta description ie.

    <code><meta name="description" value="<?php echo get_post_meta($post->ID, 'meta_desc', TRUE); ?>" /></code>

    Then when you write a post you also go to the custom field area, in the key select list you'll see meta_desc (once you've set it once it will always be in the select list) and you type your description in the text area box. It's only assigned to that specific post then.

    If that still doesn't make sense then I recommend you actually follow my post above and try it out for yourself. Then you should understand how it works.

  • Posted by Mario Andrade on 3rd Nov 2008

    Most people also don't know the real power of using the excerpt custom field on the wordpress posting page. I wrote something about it but it's in Portuguese.

    About the custom fields I mean any visitor being able to send a post and it stays in moderation until an admin approves it.

    I've been using Mini Forms plugin for that but would sure prefer to be able to do it myself.

    I reckon that I have to create a default user to allow to create content but don't have a clue on how I would go from there.

  • Posted by Sarah on 3rd Nov 2008

    Hi Mario, to allow anyone to submit a post but doesn't go live until you authorise it doesn't need a custom field.

    Allow people to register on your site and you can set the default user role as subscriber or contributor. If someone is a contributor they can write a post, edit that post but it will not go live until you or an Editor allows it (I think Editors can, but don't quote me on that!).

    I'm not sure what you mean about the excerpt custom field, or do you just mean the post excerpt. It's handy at times but I've used it more on CMSs than blogs.