Shortcodes and Custom Fields are two very powerful features of WordPress which allow you to insert any custom content anywhere on your site. But in certain cases, you may need to insert a shortcode into your Custom field. This can happen if a plugin you use only offers shortcodes but you need to insert that shortcode only on a certain portion of the theme (e.g. single.php). Now custom fields can be placed at any place of the theme but Shortcodes only appear in post content. And if you include shortcode directly as a custom field value, it will be displayed as it is without getting parsed. For example, if you insert [youtube]VIDEOURL[/youtube] in your custom field to display youtube’s video (In the Viper Video Tags Plugin), it will show up as [youtube]VIDEOURL[/youtube] instead of the video which is because it does not parse shortcodes. But then WordPress is too great so don’t worry, since a hack is there which will allow you to parse Shortcodes into your custom fields:
<?php if ( get_post_meta($post->ID, 'cfield', true) ) echo do_shortcode(get_post_meta($post->ID, 'cfield', $single = true)); ?>
In the above code, the custom field “cfield”‘s value is directly passed to the do_shortcode() function which parses the shortcode in the custom field’s value and is echoed to the browser.
December 8, 2011 at 4:39 am
I need to get the custom field, which is a youtube link and insert it into the shortcode [youtube]youtube link[/youtube]. I can’t seem to get this to work after placing it into my single.php. and modifying the cfield. It just comes up as [youtube]youtube link[/youtube]. I need to be able to open and close the youtube link with the shortcode.
December 3, 2011 at 2:08 am
How can i parse the “[gallery]” code on wordpress 3.2.1 ? I tryed ” ID, ‘[gallery]‘, true) )
echo do_shortcode(get_post_meta($post->ID, ‘[gallery]‘, $single = true));
?>” but is not workin’.
December 3, 2011 at 3:05 am
You are entering shortcode in the code itself. That is wrong. Use shortcode in the custom field’s value. And custom field’s name in place of [gallery].
November 21, 2011 at 10:47 pm
Thank you, thank you, thank you… I thought that was not even possible to use images with captions added through tinymce on custom fields. Again; thank you!
October 14, 2011 at 9:02 am
I know this is the fix I need for my problem, however I am having issues getting it to function properly. The following is the code in my theme file that calls the custom field that I am needing to insert shortcode into:
I modified the code in various ways to attempt to get the desired affect. Only thing I managed to do is get the short code to appear in the post twice and not parse to display the video short code like I need it to. Any advice anybody? Below is my most recent them code modification if that helps.
ID, 'video_code', true) ) echo do_shortcode(get_post_meta($post->ID, 'video_code', $single = true)); ?>
Sorry for the multiple posts. Forgot to use the code tag…
October 14, 2011 at 10:14 am
I still can’t see your code. Go to pastebin.com, paste your code there and give me its link. Or if you want to post code here, post it in between [php] and [\php]
October 14, 2011 at 9:21 pm
I apologize for cluttering up your comments section here…
Here is the situation. This comes from the file (singe.php) that calls on the custom field that I need to accept short codes:
<div class="entry"> <?php $video_code = thman_getcustomfield('video_code',get_the_ID()); if(!empty($video_code)) : ?><div class="video_code"><?php echo $video_code; ?></div><?php endif; ?>This is about as far as I have gotten. All I can get it to do is echo the shortcode again creating duplicate shortcode in the post rather than parsing the shortcode:
<div class="entry"> <?php $video_code = thman_getcustomfield('video_code',get_the_ID()); if(!empty($video_code)) : if ( get_post_meta($post->ID, 'video_code', true) ) echo do_shortcode(get_post_meta($post->ID, 'video_code', $single = true)); ?><div class="video_code"><?php echo $video_code; ?></div><?php endif; ?>Should this go into the functions file or the file that calls the custom field that I want to parse the shortcode? Maybe that’s my problem. The code for this theme I am using is kind of screwy so I am having difficulty getting it to function with your fix. I know this is exactly what I am needing. Any help you can provide would be very much appreciated.
October 16, 2011 at 9:19 am
Sorry for a bit late reply. The code you mentioned should go in the file where the custom field is called. And you don’t need to perform 2 checks. Your code can be simplified as
<?php $video_code = thman_getcustomfield('video_code',get_the_ID()); if(!empty($video_code)) : ?><div class="video_code"><?php echo do_shortcode($video_code); ?></div><?php endif; ?>Since you already got the $video_code from the custom field using your own function and checking it already that its not empty these checks can be avoided the second time. Try this code.
October 14, 2011 at 8:53 am
I know this is the fix I need for my problem, however I am having issues getting it to function properly. The following is the code in my theme file that calls the custom field that I am needing to insert shortcode into:
<blockquote cite="
“>
I modified the code in various ways to attempt to get the desired affect. Only thing I managed to do is get the short code to appear in the post twice and not parse to display the video short code like I need it to. Any advice anybody? Below is my most recent them code modification if that helps.
<blockquote cite="
ID, ‘video_code’, true) )
echo do_shortcode(get_post_meta($post->ID, ‘video_code’, $single = true)); $video_code = thman_getcustomfield(‘video_code’,get_the_ID()); if(!empty($video_code)) : ?>”>
October 14, 2011 at 8:44 am
I know this is the fix I need for my problem, however I am having issues getting it to function properly. The following is the code in my theme file that calls the custom field that I am needing to insert shortcode into:
I modified the code in various ways to attempt to get the desired affect. Only thing I managed to do is get the short code to appear in the post twice and not parse to display the video short code like I need it to. Any advice anybody? Below is my most recent them code modification if that helps.
ID, ‘video_code’, true) )
echo do_shortcode(get_post_meta($post->ID, ‘video_code’, $single = true)); $video_code = thman_getcustomfield(‘video_code’,get_the_ID()); if(!empty($video_code)) : ?>
October 13, 2011 at 8:09 pm
Ok but so how do you put the custom field inside the shortcode. So if you have a shortcode that you put a url inside of that embeds a video. How can you set it so all you have to is enter a url and it embeds that custom field inside the shortcode that is added to the template.
October 14, 2011 at 1:52 am
Can you be a bit more clear on what you want to achieve?
September 25, 2011 at 11:40 am
Thank you so much!!! Just what I was looking for.
August 31, 2011 at 10:46 pm
Thank you! This is a great tip and I will be using it from now on!
June 2, 2011 at 12:57 pm
Hi, thanks for the tip! One little problem I’m having though:
The code still shows up on the page on top of my image gallery. This is a shortcode from the galleryview plugin that works with Nextgen.
So, it makes the gallery show up, but with the code as well. Any ideas, how to get rid of this? Thanks!
May 31, 2011 at 10:52 pm
Excellent! This saved me a lot of time.
Thanks!
March 18, 2011 at 5:52 pm
try do_shortcode() if you only want to parse the shortcodes.
Applying the_content filters will also mean that all other content filters, like before and after content filters, will also get applied.
May 19, 2011 at 9:20 pm
Updated the post with do_shortcode() method.
May 19, 2011 at 9:26 pm
What method did you have earlier?
May 21, 2011 at 10:39 am
Applying the_content filter on the custom field. But that seemed an overkill for such a simple task.
March 2, 2011 at 3:24 am
Thanks Navjot – exactly what I was looking for for my Videopress shortcodes – Now I can put them in customized areas apart from the main post!
February 25, 2011 at 7:30 pm
Disappeared after saving the admin interface.
February 21, 2011 at 9:42 pm
Great! Thank you, been looking for this for a while.
December 8, 2010 at 10:56 pm
Sorry – I’m being a bit of a dumbass here
I can tell this is exactly what I’m after, it’s a brilliant post.
However I’m really struggling to implement it.
My understanding is that I place your code in my single.php (for example) and it will parse any shortcodes I place in my custom field.
However where I get lost is the $var bit – could you explain a little more how this particular bit works please, maybe a basic example?
Thank you again, brilliant post :)
Sam
November 6, 2010 at 3:24 am
Where and which file do I paste this code?! I can’t get it to work… Thanks in advance!
November 1, 2010 at 6:30 pm
Hi… Is it work on wordpress MU?
October 21, 2010 at 3:42 am
Hi,
This trick isn’t working when used in conjunction with MultiEdit. Here’s the code I’m trying to use:
[langcode]
ID, ‘multieditDisplay_Sidebar’, $single = true) ); ?>
[/langcode]
Unfortunately, my sidebar still spits out the raw shortcode, instead of rendering it. Very, very frustrating! Any pointers would be appreciated. Thank you!
~jenn
October 21, 2010 at 7:38 am
Pls publish the code in [php] [/php] tag…langcode was just used to show that u need to insert language’s code.
September 15, 2010 at 10:46 pm
Hi,
I have a custom field which i can echo as text in the sidebar. and its between two square brackets to form the shortcode. I then want that shortcode to behave as it would in the post body but on the sidebar.
am I right in thinking this code is what I need? if so can you please tell me where to place it please? I tried it in the sidebar in a php executable widget but no luck.
August 8, 2010 at 4:56 pm
ok … i give up :-)
August 11, 2010 at 7:05 pm
You can paste your code at http://pastebin.com/ and then post its link here.
August 8, 2010 at 4:55 pm
Hi, the code in my reply didn’t seem to paste properly ….
ID, “map”, true)); echo $map; ?>
August 8, 2010 at 4:42 pm
Brilliant!!
Thanks….
I did run into a strange little issue tho (I’m relatively new to WordPress tho so maybe its not so strange at all)
Using the “Google Maps for WordPress” Plugin my filter code looks like this…
ID, “map”, true)); echo $map; ?>
I checked the html source code of the rendered page the Map code was there, but the map wasn’t displaying.
So I put the shortcode into the content area also. Then both maps appeared…. So I then just commented out this code with regular html comment tags and VOILA!!! My map displays in the custom field but not in the main content area….. Weird…. No?? …..
July 21, 2010 at 8:36 pm
Hi
I am having trouble getting this to work, I don’t know much php.
I have the custom key: imagegallery_code with [slideshow id=3 w=250 h=250] in the value (shortcode is from nextgen gallery).
And I have this code in my functions.php within my custom single loop:
ID, ‘imagegallery_code’, $single = true) );
?>
It is not outputting the gallery, what am I missing!? I also tried “thematic_content” as im using thematic but still nothing.
Help!
Cheers
my full code is:
// My new Single Post
function my_single_post() { ?>
<div id="post-” class=”">
ID, ‘imagegallery_code’, $single = true) );
?>
<?php wp_link_pages('before=’ .__(‘Pages:’, ‘thematic’) . ‘&after=’) ?>
<?php
}
add_action('thematic_singlepost', 'my_single_post');
July 21, 2010 at 9:20 pm
Oh I got it!!
July 3, 2010 at 3:02 am
Whahoo!!
Thanks so much. This is just what I needed. to get those wp-captions to show in my custom field template.
thanks
June 29, 2010 at 1:20 am
Hi,
I guess this is what i’ve been looking for…maybe you can tell me?
I ‘m working with post types and custom fields, and want images – which are being uploaded trough Verva Metabox plugin – to open in a lightbox, once in the post.
in the above function what does the_content refers to?
what do i need to replace with?
i’m very new at this :)
thanks a lot
Maria
June 13, 2010 at 5:39 pm
Awesome chunk of code. I wish I’d know about this previously as I’ve needed to parse custom fields like this many times before.
Thanks very much :)
June 15, 2010 at 11:47 am
Glad I could help. :)
June 12, 2010 at 11:45 pm
Thanks a lot buddy.you save a lot time of me :)
June 15, 2010 at 11:48 am
I am glad it has helped u in saving time. :)
May 22, 2010 at 12:38 am
In which file in WP do you put the code?
Thanks
May 22, 2010 at 3:16 am
In the file where you will be using the custom field’s value.
April 22, 2010 at 11:18 pm
Was trying to figure out how to automate lightbox on the custom fields. This script helped me to add the necessary filters on the custom fields. Saved my life. I was tearing my hair out all day yesterday. Thank you thank you thank you!!!
April 23, 2010 at 12:35 am
I am glad it helped you out. :)