Custom Fields in Posts with Formatting using Shortcodes

October 1st, 2009

I searched for a long, long time to find out how to insert custom field values inside my WordPress posts with formatting, and I finally came up with a reasonably simple solution. Using the Custom Fields Template (CFT) plugin, I created forms to more easily enter my custom field keys in my post entries.

Then I wrote a small plugin which allows you to insert a shortcode anywhere in your posts to display your custom fields, formatted just as you wish. This plugin also checks the first custom field for emptiness, and if it is empty, then it outputs nothing. The other benefit of using this method is that all of the custom fields will be shown in your RSS feeds, which is nice. The Custom Fields Template plugin actually includes an experimental feature which lets you do something similar to what my method does, but I had great difficulty with it due to limited PHP (and particularly IF statement) support.

To illustrate the benefits of this approach, here’s a simple example:

Let’s say you have two custom fields in your post which contain links to photos you want inserted inside your post. Once you create the CFT, and input the links into the respective CFT fields, you can insert the photos with the proper formatting by including the [postphotos] shortcut (you can also call the postphotos_function in your template if you want your output outside the post content). My sample plugin just loops through all of the custom fields included in your post and then places them within a div styled through CSS. All you have to download the plugin and edit the formatting. You can also have the plugin ignore certain fields by placing their names in quotes and separated by commas in the following line:

$ignoredkey = array("ignore_me1", "ignore_me2" );

It should be noted that this can be done inside of your functions.php theme file as well, but I thought it cleaner to do it with a plugin. I want to say a big thanks to Gabriel for his help and original CFT post which inspired this work-around.

Coming up with this method has once again assured me that WordPress is the ideal platform for like 90% of sites out there. Hope that this post helps some of you, and if you have any questions, leave a comment and I’ll get back to ya. Otherwise, I’d love to hear your thoughts on this method (I’m sure there’s a better way to accomplish this).

[update 10.03.2009]: I have updated this plugin (to version 0.2). It now accomplishes the exact same thing much more efficiently (with loops) and does not require as much hard coding for each individual custom field. I also added an ignore list where you can specify which custom fields you don’t want to include in your post. Almost every step is commented and explained, so let me know what you guys think or if you have any questions.

Leave a Reply

random tips & tricks

this is where I share with you (my two readers) my posts which have no better category.

take a look around and be sure to let me know in the comments which posts you find helpful.