Display WordPress Custom Field Values Within Post Body

Title image for 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).

Comments on Display WordPress Custom Field Values Within Post Body

Web Designer

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.

Posted by Web Designer (1 comments) on 26th March, 2009 at 4:54 pm.

Rob Barrett

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.

Posted by Rob Barrett (45 comments) on 26th March, 2009 at 7:14 pm.

Peter

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.

Posted by Peter (1 comments) on 31st March, 2009 at 4:14 am.

Bryan

This is exactly what I was looking for!!! Thanks for this post on inserting custom fields into a wordpress page.

Posted by Bryan (1 comments) on 11th April, 2009 at 9:16 am.

Rob Barrett

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.

Posted by Rob Barrett (45 comments) on 11th April, 2009 at 9:53 am.

UK Short Breaks

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!)

Posted by UK Short Breaks (2 comments) on 16th April, 2009 at 8:56 pm.

UK Short Breaks

slight typo on the previous post, I meant I edited the PHP page to show the expiry date, if present, automatically!

Shalina

Posted by UK Short Breaks (2 comments) on 16th April, 2009 at 8:58 pm.

the_guv

cheers Rob – top tip … shortcodes was just what I wanted, real handy.

Nice site too … me likee lots.

catch you on Twitter.

Posted by the_guv (1 comments) on 3rd June, 2009 at 1:12 pm.

Rob Barrett

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!

Posted by Rob Barrett (45 comments) on 3rd June, 2009 at 1:35 pm.

Jesse

Great wordpress plugin! It can help me save alot of time. thanks!

Posted by Jesse (1 comments) on 26th October, 2009 at 11:35 am.

Chicago Printing

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

Posted by Chicago Printing (4 comments) on 28th February, 2010 at 7:03 pm.

Curtis Penner

Thanks for the tip. Nice and Easy. Just what I needed.

Posted by Curtis Penner (1 comments) on 25th March, 2010 at 7:37 pm.

Trackbacks for Display WordPress Custom Field Values Within Post Body

Leave a Comment





Rob Barrett Introduction