Enabling shortcodes for custom fields

So, in this post it is explained how to supposedly enable shortcodes for custom fields in WordPress. (Shortcodes are not enabled for custom fields by default.)

What I dont understand is where to place this code:

<?php echo do_shortcode(get_post_meta(get_the_ID(), 'name', true)); ?>

I understand I have to paste the name of my custom field into the ‘name’ parameter, but where do I need to place this line of code?

I suppose in the functions.php of the template? I am unsure where though in that file. I don’t want to break anything of my template.

Any suggestions are appreciated.

1 Answer
1

Normally WordPress does not run shortcode that you put into a custom field.

By default, Custom Fields display whatever value you enter, as plain-text, so if you try entering a shortcode, (in the format [shortcode] VALUE [/shortcode]) you’ll end up displaying the entire text, including the tags.

Add the following in your template file, it can be single.php or page.php:

echo apply_filters('the_content', get_post_meta($post->ID,'YOUR_CUSTOM_FIELD_NAME', true));

or…

$shortcode = get_post_meta($post->ID,'YOUR_CUSTOM_FIELD_NAME',true);
echo do_shortcode($shortcode); 

Leave a Comment