I was talking with someone today and they had a question about some setting in their WordPress site that they needed to update, but were not sure what to do. I told them, “you just need to update the value of some xyz option and you will be good to go.”
After about an hour, they called back and said they had looked high and low in the admin area, and that option doesn’t exist. They then proceeded to say they were trying to find a way to log into the database to see if they could set it there (which is ALWAYS a bad idea)! Of course I know it exists. I know what it is used for. I know how to update it. But the thing I forgot all about, and the thing I take for granted, is that I know where to go to see all the available options for WordPress in one place – and they didn’t. And boy do I remember those days!
One of the most useful things about WordPress is the ability to add custom fields. Much like myself, those just starting out with WordPress don’t usually fully understand what Custom Fields are or even how to use them.
Several years ago when I first started out with WordPress, clients were always asking what it was – and my canned response was, “you won’t need to worry about those.” Fast forward a few years and all has changed. Now, when I set up a custom WordPress site, you can almost rest assured there will be Custom Fields in use, and in some cases, dozens of them.
So that still doesn’t answer what Custom Fields are. WordPress’ definition of them is this:
WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data.
With some extra coding, it is possible to achieve more complex actions, such as using the metadata to store an expiration date for a post.
Meta-data is handled with key/value pairs. The key is the name of the meta-data element. The value is the information that will appear in the meta-data list on each individual post that the information is associated with.
Keys can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a “reading” key and use it twice on the same post, once for each book.
Ok, so what the heck does that mean? Well one might assume that the meta-data has something to do with Search Engines and what they look for when they crawl your site. After all, we do put that stuff in META tags, right? Logically, that would make the information we put in them META DATA, right? Wrong – at least in this case.
Well – if you want to get technical, I suppose you could use Custom Fields to add meta-tags (the correct term), to your site. In fact, many plugins do just that, but in this case, the Custom Fields and the meta-data we are referring to are something completely different. The meta-data in Custom Fields is intended to enhance your site and simplify the user or admins interaction with the site.
So, when asked, here is how I describe Custom Field:
A Custom Field is a way for you to add extra content or information to your site. They also allow us to make a site more dynamic, by adding options or variables to an individual page or post.
Ok, so you may still be saying, “What?”
But in reality, my response is much more accurate. Because that is what they are used for - to enhance you site.
Now to explain what they really are in technical terms. Custom Fields are bits of data – either strings of text or numerical – that are stored in the database and reference the page or post you have them added to. There are two parts to a Custom Field – a KEY and a VALUE. The key is the name of the Custom Field, and the value is the data (remember, the string or number). You can add as many Custom Fields as you want to any page or post.
So once you add a Custom Field, that’s all there is to it and you can display the information right away?
The answer is No. The kicker is – you CAN’T use a Custom Field unless you or someone else, via a plugin or theme, has programmed WordPress to use it. DAMN! So now I have to be a programmer to use Custom Fields!
It sounds worse than it really is. Most of the time the Theme creator or Plugin programmer has already added ways for you to use specific Custom Fields. Like, “thumbnail” for example. That is a common theme and plugin Custom Field that is used all the time. You simply add the URL for an image and the theme or plugin puts it where it needs to go – because it was programmed to do that using a Custom Field with a KEY of thumbnail and a VALUE of the image URL you add.
I want to show you how simple they really are to use, so I am going to give you some real life examples. BUT, and this is a big but, keep in mind that if you start playing around with the programming code of your site, you really can screw something up. So have a backup of your theme files and backup your database before you touch anything else!
Now, on to the meat.
First, add the Custom Field to you page or post.
Now for the fun part – adding the Custom Field data to your theme.
the_content() function is used. When you find it, place your cursor either right before it or right after it – depending on if you want your Custom Field data to show before or after.<?php the_content('maybe something here'); ?>, then you want to edit just BEFORE the <?php or just AFTER the ?>.... some code here
the_content('maybe something here');
... some more code here
then place you cursor just before the the or right after the ); and press ENTER to give yourself a new line to work with.
<?php, you will add the following code (before the <?php or after the ?>):<?php echo get_post_meta($post->ID, 'your-custom-field-name-here', true);Â ?>echo get_post_meta($post->ID, 'your-custom-field-name-here', true);You can get pretty complex and assign the Custom Field VALUE to a variable and use it someplace else or perform other functions on it, or even use it to display a completely different set of information if it is present. I’ll add a few examples at the bottom of the page, but hopefully this opens your mind up all of the possibility you have, and to how powerful Custom Fields can really be.
To add Custom Field to a variable:
<?php $myCustomFieldValue =Â get_post_meta($post->ID, 'your-custom-field-name-here', true); /* <-- to assign the variable */ ?>
<?php echo $myCustomFieldValue; /* <-- to use the variable */ ?>
I use this exact method for the top of the page marker that says “This post contains a Tutorial”:
<?php
//assign the Custom Field to a Variable
$checkTutorialLevel =Â get_post_meta($post->ID, 'tutorial-level', true);
//Then check to see if if has a VALUE
if($checkTutorialLevel!=''){
//if it does, display Tutorial Marker div
echo '<div class="tutorial-marker">This post contains a Tutorial with ' . $checkTutorialLevel .'</div>';
} ?>