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.