Execute shortcode in custom field

DescriptionWordpress snippet by - 03/30/10

This snippet will enable you to add shortcode to a wordpress custom field. Normally wordpress does not run shortcodes that you put into a custom field. Just put this code into whatever page you are displaying the results of the shortcode, and change the 'your_custom_field_here' to the name of your custom field.

Tags ,

<?php echo apply_filters('the_content', get_post_meta($post->ID, 'your_custom_field_here', true)); ?> 
  • Share
Submitted by: Adam J Nowak
http://hyperspatial.com

39 Responses to “Execute shortcode in custom field”

  1. Thanks for the tip, this helped me out big time!

  2. Thanks a lot buddy.really helpful :)

  3. Great!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Pushparaj says:
  4. Wow,

    You helped me solve a problem now that has been going on for weeks! What a relief! Thank-you so much! I was almost going to give up.

    Thanks again!

  5. Every time I write some interesting php for a custom field I put it in the collective.
    They all get filed under the tag custom fields for quick access.
    Don't forget, you folks can store your own snippets on this site, just register and start storing them here.

  6. perfect ! you help in a problema that did take me more than a day ! Thankyou

  7. Where do I put this code?

  8. Typically this code would go into one of the wordpress template files that displays the blog posts. Like single.php or archive.php

  9. Hi nice trick

    But i use wp estore plugin for ebook store and i have to put shortcode with product id in post and it shows the paypal button for that product, is it possible that i just add id in custom field and it shows that button.

    Means a custom field which add a shortcode itself and will put product id as value.

  10. Not quite sure what you are getting at, but the shortcode you put in the custom field should run the same as if you were to put that shortcode into the post body.

  11. Thanks! Just what I needed!

  12. This was a life saver...you rock!

  13. Thanks this worked like a charm! :)

  14. hello,
    if i want to insert custom field value into the_content() how i do that please??

  15. That is the purpose of this exercise, look through the thread above. The snippet goes in your template file, like single.php - you need to change the name of the 'your_custom_field_here' to the name of the custom field you want to display that is connected to the post.

  16. amazing...short and simple ! thanks

  17. Hi, will above approach work for genesis framework? I have tried but it really greats a mess. Thanks

  18. I have no experience with that framework but this code is all php/wordpress core - so it should work on any wordpress install unless the theme or a plugin interferes.

  19. Thankyou so much, this helped immensly!

  20. Another happy customer, thanks buddy!

  21. Thank you so much for that tip!

  22. This code works great, however when I insert it into my custom post template, it ends up generating two "ends" of the document, and other plugins which add content to the end of the post (social buttons, post signature plugin) get duplicated.

    How do I solve this?

  23. Put the snippet it a template file before the loop, say in single.php, if you are adding it to a functions file or an included php file that may be the issue.

  24. Solid! Thanks for the fix, never needed to do this until now. And thanks for being so findable on Google, too :D

  25. Could you supply a variation for those not wishing to print results as part of "the_content"?

    J

  26. Never mind. It looks like it renders the output where ever you place the code. Not as part of the_content. Thanks.

  27. You Saved my life!!!! Or at least my sanity ;P I've been scouring the internet for exactly this little tidbit, and with a deadline getting dreadfully close. Thanks so much!

  28. You Saved my life!!!! Or at least my sanity ;P I've been scouring the internet for exactly this little tidbit. Thanks so much!

  29. Good deal. I think this snippet has been the most "life saving", based on the comments at least.

    If you do a lot of shortcode you must try this trick I came up with http://code.hyperspatial.com/1105/function-as-shortcode/

    This allows you to print php normally in the callback functions instead of "returning" the HTML

  30. Hi, this is a great tip and it solved my problem !

    in the post page it seems to work ok but when i try to call the shortcode in a widjet with recent posts i cant get it work

    my case is a countdown timer which is generated in each product by custom fields (which you gave me the solution)

    if is another way to call the snippet and try it would be great !

    Thanks

  31. I would try a filter for the_excerpt instead, more than likely a recent posts widget will call up the excerpt instead of the content.

    Otherwise I would just do a recent posts widget from scratch, and use do_shortcode()

  32. Thank you for your responce ! Unfortonatly i am not a code expert and i cant make a plugin from the scratch.

    the onlyway is to call somehow this code to show in the widget .

    if you like i can sendyou the code just to take a look

    Thanks again

  33. Thanks for the info! Just what I needed for a theme we're working on.

  34. I needed something like this for a template theme im working on with cart66 plugin! Thank you!

  35. This code works great !

    Thank you very much !

  36. Hi,

    I am trying to put a [shortcode] inside a repeater field, but I cannot make it work. The code you gave here it works fine for a normal field, but how do I use it to display a repeater field?

    thanks!

  37. Have never used them. Look for a filter. If you figure it out feel free to post the solution here!

Comments and Feedback