Redswish

Carefully crafted banter

The power of WordPress Custom Fields

Custom Fields
I can’t believe I’ve been playing with WordPress for so long and never explored the Custom Fields feature. A website I’ve been working recently has required me to push the boundaries of WordPress, or more appropriately my understanding and knowledge of it. In doing so I’ve bravely slipped deeper into the Advanced Options and lived to tell the tale – the tale of the Custom Fields…

The fundamental functionality of Custom Fields provides you with the ability to expand your posts and their content. They allow you to include extra information and features in your posts that the WYSIWYG editor doesn’t provide itself.

How it works.

When in your Write/Manage posts page, mooch down past Tags and Categories to the Advanced Options – ooh! Down here in these murky depths you’ll encounter the Custom Fields box. In here you’ll see 2 empty textareas labelled key and value.

The key is the name for your custom variable, and the value is, well, it’s value! When you create a custom field, you are creating new meta-data. You then insert a simple line of php into your WordPress template where you want this extra data to appear in the theme and voila! You’re done.

The Process

So, the easiest way to show you is with a short example.

Adding Subtitles to your post

I feel subtitles serve more as a presentational feature than anything. And sure, you could always just insert a h2/h3 at the top of your post to act as a subtitle. But sometimes that doesn’t cut it, especially if your subtitle needs to stand out from the post content more than an standard inline header.

Step 1 – Theme files:

In your WordPress theme, you need to enter a line of code to pull in your custom field. Make sure this is within the post loop, the most obvious place is of course directly under your post title. Here you go:

The only bit you may wish to change is the keyword within the parentheses. In this case ‘Subtitle‘. This ties in with the key you create in the post editor, so make sure they’re the same. And only use it again if you wish to display the subtitle somewhere else, for any other custom fields you’ll have to change this.

It would make sense to place this code within header tags:

That’s it for the code!

Step 2 – Post page:

When you’re writing or editing a post, go down to the Custom Fields area in the Advanced Options. Create a key which correspondes to your keyword you set in the code. The value will be the actual Subtitle content. So for example: Key – Subtitle Value – An inside look at Google’s new browser… Add your custom field and save your post.

Step 3 – Check it out:

Check out how it displays on the page. Then utilise the power of CSS to get it looking bob on!

Having more fun

Let’s face it, subtitles aren’t that interesting. But there’s a lot more you can do, a lot of which I’ve still yet to discover. Post Thumbnails

Add the following code to your theme:

" alt="" />

This will include an image, pulling it in from a specific folder. So if you upload all your thumbnail images to the directory /wp-content/post-images/ and then in your post editor set the key as ‘Image-Thumb’ and the value as your image name (eg. picture.png), this will pull in your thumbnail picture and place it in your post. You can control the appearance of the image with CSS.

To actually upload your images to the specified folder, you can use good old FTP or a plugin I’m particularly fond of, because it keeps you in the WP admin area without having to use any external programs: Filosofo Old-Style Upload.

Of course this doesn’t necessarily have to be a thumbnail, it can be a full size image, adding plenty of colour and interest to your blog articles.

For a more advanced alternative to this method, check out Justin Tadlock’s method.

Meta-Data

Some of the examples given on the WordPress Codex page on Custom Fields are the simplest, and the best. What if you fancy adding information relating to your current mood or the weather at the time of writing your post? Try:

Current Mood:

Key: Mood
Value: Happy
Code:

Current Mood: 

Displays: Current Mood: Happy

Listening To:

Key: Listening-to
Value: Fly me to the moon – Frank Sinatra
Code:

Listening to: 

Displays: Listening to: Fly me to the moon – Frank Sinatra

Other ways to display meta-data

To pull in all the meta-data for a post in one fell swoop, go for:

Which will display all the meta-data in an unordered list and add’s classes that can be styled with CSS.

If you want to pull meta-data from a specific post, you can specify which post by the post ID, which data by it’s key and whether to display the data as a single item or pull in an array:

  • $post_id is the ID of the post you want to select data from
  • $key is the name of the meta-value you want
  • $single can be either true of false. If true it will pull in a single string of meta-data, if false it will pull in an array of the custom fields.

Go Play…

Like most things, Custom Fields open doors which you have to walk through and explore. In essence they’re very simple but if used correctly can be pretty powerful, another extension of the basic functionality of Wordress. So go forth and experiment!

For more information on Custom Fields and all things WordPress – visit the WP Codex.

Comments & Opinion

40 Responses. Have your say.

  1. I love this post! I am just now beginning to explore the custom fields feature for thumbs, and am glad to see a tutorial on doing even more. Thanks!

  2. Jin, on , said:

    Good post.

    I use custom fields extensively on my blog entries, since they all have different background images and title color. That’s another reason I love WP.

    I’d like to point out, I think a null value check should be in place, before outputting the value.

    For example, the following code determines what bgcolor to use for a particular blog entry. if no custom field is assigned, it uses a default value. (i’m not using brackets, in case it doesn’t go through the comment)

    $bgcolor = get_post_meta($post->ID, ‘bgcolor’, true);
    $bgcolor = $bgcolor ? $bgcolor : ‘#888888′;

    /– inline header css –/
    background-color: ?php echo $bgcolor ?;

  3. That’s a nice cheat-sheet.. Bookmarked for future reference :)

  4. Cool, i never really thought about what those custom fields could be used for until now.. but i could create a whole set of classes to use for specific posts.. that’s pretty sweet. Do you know if you can make function calls with those fields as well?

  5. nathan, on , said:

    Thanks for the comments, I’m glad this tut is of use.

    @Clay – this article strained my programming knowledge as it is. I’m not sure what you mean by function calls?!

  6. James, on , said:

    Nice post nathan, As to clays post i doubt that you can use function calls within these, Just so that every one is clear a function call is where you call one of the statements within the system an example is get_post_time(), I may be wrong just dont have the time to test it, By the way clay what would you want to achieve via this anyway.

  7. very usefull. thanx. :D

  8. adkdev, on , said:

    Thanks a lot…

  9. I’ve always wondered what I might be able to use those custom fields for but I never took the time to look into it. Great post! Thanks!

  10. Thanks! I’ve been wanting to learn more about custom fields. This explains a lot.

  11. @james.. well i’ve been working on my own system that builds on top of wordpress.. it’s for a client that gets high traffic and rather than a generic contact form, they want a few different contact forms that would relate to different posts. For example, the site is a sports networking site.. They have 5 writers for up to 10 different sports that all post in the same blog. Sometimes they want to rate feedback or do a poll but only for a specific post.. SO i was looking for a way to call different functions to pull in those post-specific forms.

    If this plugin doesn’t do that know of any that would..? I figured i’d probably have to write it myself because i doubt many have a use for that

  12. I don’t *think* you can use it to call functions. But! You could define a special key that when it has a value would trigger a switch statement that then reads the value of your custom field and runs a specific function based on the value. I am responding on my mobile, and not ambitious enough to put together a code sample. if you have questions e-mail me and I will make another post here. nathan@devstu.com

  13. Mmhmmm, that’s not a bad idea, I may do that indeed. No need for code examples, I know what you mean. Thanks :)

  14. Chad, on , said:

    You may enjoy the Custom Write Panel plugin. :wink: Ever since I discovered the power of Custom Fields, this plugin as well as the RC Custom Fields plugin have been awesome.

  15. FAWiKi, on , said:

    Hi! The post is really interesting! I’ve read your blog and can say it’s a good job.

  16. Great blog. I like layout!!!!

  17. Jason King, on , said:

    So thts wot those custom fields were for?! Nevr realised their potential til now, Ill c wot I can do with them in future..

    Top site Nathan, tnx!!

  18. Johan Dole, on , said:

    Thanks for a pedagocial article.

    Have you heard about Flutter? Seems to be an incredble Custom Fields-plugin.

  19. Celosia, on , said:

    Thank you! The theme that I’m currently using has a custom folder containing a custom CSS file and a Custom functions file (custom_function.php). Just want to know if it’s possible to just modify the custom_function.php instead of modifying the main theme files.

  20. nathan, on , said:

    Thanks again for the great comments and advice.

    I’m afraid this is the extent I’ve really played with Custom Fields so far, I’ve not tried any plugins and @Celosia I don’t really know anything about custom_function.php! If I had time I’d look into it all for you but I’m afraid I’m just really busy at the moment.

    Thanks guys, I’ll get another decent article out soon.

    Nathan

  21. liz, on , said:

    very cool! I’ll have to try this out! :)

  22. Great article very useful thanks!!!

  23. Thank you for your site :)
    I made with photoshop backgrounds for youtube, myspace and ect..
    my backgrounds:http://tinyurl.com/6ptkxd
    all the best and thank you again!

  24. Hi!
    I made on photoshop anime myspace pictures.
    take a look at them:
    http://tinyurl.com/5w2eqc
    Thank you for your site ;-) xoxoxo

  25. Pingback: redswish - a web design blog » Speaking at Manchester Wordpress User Group

  26. Pingback: A Collection of 15 Useful WordPress Tutorials for 2008 » Papertree Design

  27. Pieter, on , said:

    I’ve been using custom fields in a lot of projects lately and they’re a great problem solver!
    You should definitely think of these first when you bounce upon a WordPress problem..

  28. Eric, on , said:

    @Johan Dole
    WOW! I had not heard of Flutter before. Absolutely incredible. Thank you for the great article Nathan and thanks for the comments all. Good stuff.

  29. Bud Greenspan, on , said:

    Step 3 – Check it out:
    Check out how it displays on the page. Then utilise the power of CSS to get it looking bob on!
    ——————-
    I’d love to check it out, but you didn’t provide an example.

  30. I’v heard of WP custom fields but before never understood what they did. I now have a much better understanding. Many thanks for this post.

  31. nathan, on , said:

    @Bud Greenspan – the idea is that you try it out for yourself!

  32. Thanks for this great info i use joomla myself but i am considering wordpress so when i do use it i can implement this info.

  33. I must admit, very useful tips. Thanks a lot.

  34. webtha, on , said:

    Thanks for shared great info.

  35. Wow, very cool info.

  36. ricette, on , said:

    very interesting info about the custom fields. Thanks

  37. Oh great its very good information. I need to know some information about word press for this I got very good information.

  38. Brian, on , said:

    Thanks for your share… Much appreciated.

  39. I’ve been playing with WordPress for so long and never explored the Custom Fields feature.

  40. I never knew that wordpress custom fields can be used for all these purposes

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>