3

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.

2
  • 1
    I really don't understand what you are trying to do. Shortcodes should not be executed inside a custom field. Why do you need to do this. BTW, if you are using do_shortcode(), then you are doing it wrong. Commented Jun 15, 2015 at 8:30
  • I agree it's a bad design, but within my template this currently is the only solution for a certain problem. I need to be able to use a shortcode inside of a custom field. It must be possible, isn't it? Commented Jun 15, 2015 at 8:48

1 Answer 1

10

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); 
5
  • 1
    hm, I cant get it to work. I gotta admit I don't have the best understanding of how the wordpress framework works under the hood. I tried both your suggested solutions, but neither worked for me. I tried them in the templates page.php file and in the single.php file. Pasted them in the top within the first php-tag. Maybe my template just can't work with these codes. Btw.: I'm using the custom field on my blog page where all the blog posts are shown together in a preview format. Commented Jun 15, 2015 at 15:21
  • Custom fields doesn't work properly on Preview Format. You should save your work before testing. Commented Jun 15, 2015 at 17:46
  • -1 Downvote Don't do shortcode kovshenin.com/2013/dont-do_shortcode Commented Aug 4, 2015 at 8:36
  • 1
    @BradDalton Kovshenin's argument is that you shouldn't use do_shortcode() when you can use the shortcode's callback function directly, so the advice does not apply to the current question. So I'm downvoting your downvote by upvoting the answer! Commented Nov 7, 2018 at 16:04
  • Is there a way to do this without modifying a theme template file eg via a simple plugin?
    – Drewdavid
    Commented May 2, 2021 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.