How can we get custom fields to display shortcode on our pages?
I saw this posed on the StudioPress support forums and it’s a very good question. The default custom fields meta box provided by WordPress strips out shortcodes from custom field values. This means you cannot, by default, use shortcodes within the values of your custom fields. For example, say you want to use a Gravity Forms shortcode to output a form in a custom field. WordPress will not render this shortcode.
Fortunately, there’s a small trick you can use to solve this: the do_shortcode()
function. By running values through do_shortcode(), it tells WordPress to render shortcodes as it would if you inserted the shortcode in the content editor (because the content added within the editor is passed through this very function). Here’s an example:
echo do_shortcode( get_post_meta( get_the_id(), 'custom_field_key', true ) );
This line of code retrieves the value of a custom field with the key of custom_field_key from the current post. It then runs it through do_shortcode() and echoes it out. Any shortcode that is used in the value of a custom field (for any post) with that key will then render properly. Pretty simple, right?
Check out this post of you want to do the reciprocal and use custom field values within shortcodes.
Alex says
Hi Ren
any idea how to do it in functions.php?
paul b says
Excellent, thanks for the help.
I just want to note i did not past this code in the functions.php file, i posted it in the actual wp template itself.
Ren says
Hi, Paul. You are correct; this should be used in a template file when outputting the value of a custom field.
Rubén says
Thank you very very much Ren: I was starting to think about a workaround for accomplishing this, never thought it would be this easy until I found your code.
Thank you again!
Lonnie Ray says
I am not much on posting comments but thank you! I was going mad trying to figure this out. Your answer was simple and elegant, unlike anything I found on StackExchange…
Daniel says
Nevermind I got it! Thanks!
Daniel says
When I use this code, the shortcodes do work, but all the HTML in the custom field become special characters. So the HTML end up appearing on the page. I mean, for example:
becomes
<hr />
So on the page, instead of seeing a horizontal line, the user sees the code. Any idea how I can fix this?
Venkat says
Hi,
which file i have to put this code??
Ren says
In your theme/plugin files, wherever you’re outputting the value of the custom field.
A guy in Winnipeg says
I was trying to relocate a div up above the post title using a custom field and php, but I couldn’t find a way to get the custom field to accept the php – then I remembered the the plugin (PHP Code for Posts and Pages) I use to put php in my WordPress posts can turn php into shortcodes, so I went looking for a way to make custom fields accept shortcodes and voila – I found this post – and I was finally able to relocate the div!
Thank-you Ren!
Paul
Stacie says
YOU ARE AWESOME BRO thank you!!