WordPress Custom Fields

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 :)

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on November 2nd, 2008 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx

8 Responses to “WordPress Custom Fields”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Clare says:

    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.

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

  3. Sarah says:

    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?

  4. Mike Huang says:

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

    -Mike

  5. Sarah says:

    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.

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

    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.

  6. 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.

  7. Sarah says:

    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.

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Find Out More About Our Newsletter
 

Blogging Tips Sponsors

Blogging Tips Newsletter

 

Blogging Tips Sponsors

 

Latest from the Blogosphere