Display WordPress Custom Field Values Within Post Body
This is such a cool little plugin that I just had to share it. I remember reading somewhere, a little while back, about a way to insert the contents of a Custom Field into the body of a Post or Page. Unfortunately I didn’t bookmark the tutorial, and when I came to need it today, could I come across it again? No chance!
You might be asking why this would be a useful feature. Well, take my specific example, for instance.
I wanted to include the code for a PayPal ‘View Cart’ button in a Page that I was building for a client, but the Visual Editor was messing up the long code lines whenever I saved. I could have deactivated the Visual Editor from the user profile, or I could have installed the Exec-PHP plugin and referenced in include file containing the PayPal code, but it was important that the page be as easy for my client to edit as possible, so those solutions were far from ideal.
What I really wanted to do was create a Custom Field called “PayPal View Cart Code” and set the code snippet as its value, and call that into the Page — all from within the Edit page.
After a couple of hours worth of searching for a method, I was ready to give up and use a workaround — but then one of my Twitter friends coincidentally tweeted about a page on WordPress Shortcodes, which lead me to the find the ‘Custom Fields Shortcode’ plugin.
Once installed, all you have to do to display the contents of a Custom Field is wrap the Custom Field Name in a [cf] tag. So, in my example:
[cf]PayPal View Cart Code[/cf]
Because the contents of the Custom Fields aren’t subjected to WordPress’ text preformatting, the code works perfectly. Plus should the HTML-unfamiliar client ever need to change his PayPal code, he can easily copy-and-paste the replacement code into this Custom Field instead of searching through the Editor. I think it makes for a pretty elegant solution.
It’s a small plugin, and you could easily take the code and place it in functions.php if necessary (and/or change the shortcode).



Pingback: Daily Links | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?
As my experience shows, some clients do not understand even what the code means. Think about it. We should be able to explain everything in common words.
Comment by Web Designer at 4:54 pm, 26th March, 2009
Thanks for the comment. I agree that clients should be taught with as much plain English as possible, but I disagree with your complaint about this method, and that everything should be taught in ‘common words’.
Firstly, there are technical aspects to web design, even for clients using WYSIWYG editors, and in agreeing to manage their own content, I believe they should have (or be educated with) a basic knowledge of HTML at the least, and that will invariably include terms which some people will class as ‘technical’.
Secondly, this post isn’t aimed at clients, but as something that other web designers may be able to make use of. It’s down to them to properly explain to their clients how to operate whichever platforms are set up for them.
Comment by Rob Barrett at 7:14 pm, 26th March, 2009
Interesting approach – I end up using custom fields on quite a few client sites, and it hadn’t ever occurred to me to give the users this kind of control. Anytime you can remove the possibility of the client messing up code (and they always do), you’ll save yourself headache down the line.
Comment by Peter at 4:14 am, 31st March, 2009
This is exactly what I was looking for!!! Thanks for this post on inserting custom fields into a wordpress page.
Comment by Bryan at 9:16 am, 11th April, 2009
No problem, Brian! I know it took me a quite a while to figure out a way to do this, so I’m glad you found this helpful!
I definitely agree, Peter — keep it simple, and there’s far fewer things that can go wrong.
Comment by Rob Barrett at 9:53 am, 11th April, 2009
Really useful, I stumbled across this method whilst trying to insert expiry dates into offers I was posting online…by placing them in the custom field i managed to edit the post PHP page and insert the expiry date manually (plus i used it for a few other cool things like adding a logo to posts, choosing flash movies to run and setting style elements!)
Comment by UK Short Breaks at 8:56 pm, 16th April, 2009
slight typo on the previous post, I meant I edited the PHP page to show the expiry date, if present, automatically!
Shalina
Comment by UK Short Breaks at 8:58 pm, 16th April, 2009
cheers Rob – top tip … shortcodes was just what I wanted, real handy.
Nice site too … me likee lots.
catch you on Twitter.
Comment by the_guv at 1:12 pm, 3rd June, 2009
Cheers, guv! I’m glad you found it useful.
Thanks for the kind words about the site as well — always a nice thing to hear! And hey, welcome to Twitter!
Comment by Rob Barrett at 1:35 pm, 3rd June, 2009
Great wordpress plugin! It can help me save alot of time. thanks!
Comment by Jesse at 11:35 am, 26th October, 2009
That is really a useful plugin. I will also look on that and see if I will also use that one. Great post.
Thanks,
Rommel
Comment by Chicago Printing at 7:03 pm, 28th February, 2010
Thanks for the tip. Nice and Easy. Just what I needed.
Comment by Curtis Penner at 7:37 pm, 25th March, 2010
This worked great! Saved this WordPress newbie from tearing his hair out.
Comment by patrick at 7:49 am, 21st August, 2010