Oct
7
2010

Creating WordPress Custom Field Boxes From Existing Meta Data

Coding custom fields in Wordpress used to take a lot of time. Now there are plugins that can help you. This brings Wordpress further into the realm of a true CMS by allowing you to create custom meta field entry boxes and define them on specific post types.

Share
Email

I always look at a systems API/hooks to see if I’d have more control over something with a minor amount or more effort by hand coding it. WordPress custom field boxes no longer have to be done that way after discovering Verve Meta Boxes.

Verve Meta Boxes VS CCK (Drupal)

If you’ve used Drupal before this is the equivalent of having the CCK module installed. Though out of the box CCK in Drupal 6 is much more flexible and configurable. If your CMS needs are dependent upon additional entry field types that are extensible through other existing plugins and functionality then you’re no longer looking at WordPress. But I digress, this is a huge leap forward for WordPress CMS development.

Convert Existing Custom Fields

One thing I had to figure out for myself was how to convert my existing custom field meta data into the separate fancier boxes Verve Meta Boxes use. Much easier than expected. Remember to backup your database before you start trying to convert existing fields though. You will probably mess this up at least once while learning all the caveats. I test everything in an XMAPP or VMWare Dev environment first!

That aside lets assume you have a custom field already called “videoclip”. You’re probably used to seeing it like this.
Wordpress Custom Fiends Standard View
With Verve Meta Boxes you’ll be able to go to Admin >> Tools >> Verve Meta Boxes and add boxes/fields on this screen. The first “Create Box” is for the box name. Purely aesthetics. The part WordPress uses when you call your meta field into your template is contained within the “Add Field” area. Name things accordingly if you want to pull from your existing custom fields already defined. You can see where I put the “videoclip” name to utilize it from my existing setup.
Editing in Verve Meta Box
By configuring the above, you’ll notice a new box within your “New/Edit Post” screens. It turns into its own seperate element as a movable box. This box will now show up regardless of it being filled out or not.
Verve Meta Box Example
You’ll notice I added a “videotype” field as well to show you how the system allows similar grouped fields in a single box. If you extend some PHP code you wont have to ever define anything like that since you can extract video types VIA preg_match on the URL’s domain.

// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
 "http://www.php.net/index.html", $matches);
$host = $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";

This example is pulled from the one on php.net.

Displaying Custom Fields In Your Template

You can access key/value data in a few ways from WordPress. You’ll find those functions outlined further within the links on WordPress’s custom_fields page. My favorites are get_post_custom_values for multiple value fields.

$values_array = get_post_custom_values('videoclip');
echo $values_array[0];

Or if you’re pulling a single text or textarea field for example use get_post_meta.

echo get_post_meta($post->ID,'videoclip',true);

Both of these belong in your single.php template page and should be placed within the post loop.

Verve Meta Boxes Bugs

There are several quirks with this plugin, but not so many you cant use it. Many of the ones that bothered me are already listed in a wordpress.org topic post.

The worst of them results in losing all your meta data if you:

  1. Create an field for an existing custom meta field.
  2. Realize you created the type as text and want it as text area perhaps?
  3. Decide to delete the field and recreate it.
  4. OOPS!
  5. Realize deleting the field through Verve Meta Boxes removes meta data for that field name across all your posts.

If you really have to update the description or post type you’ll need to jump into your database (phpmyadmin probably) and locate the wp_verve_meta_boxes and wp_verve_meta_fields tables. There should be an option built in for changing these. For me its just an inconvenience. For a novice… this is why fun toys like this never see the light of day.

All said and done, if you know about the bugs you’ll know what not to do and can continue use of one of the best CMS based plugin enhancements I’ve seen since many started jumping on the post type bandwagon. Plus this is a free plugin after all, made available by AvenueVERVE. I look forward to further updates! If  you’re up to suggestions, a “Post Reference” field like the one in Drupal’s CCK and grouped multifields would be supurb. In fact it would level the playing field in that area between WordPress and Drupal significantly.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.


Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois