Parse Shortcodes in your Custom Fields

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 in your custom field to display ‘s video (In the Viper Video Tags Plugin), it will show up as 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:

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.

46 Comments

  1. Anonymous

    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.

  2. Kidzcreativ

    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’.


    • 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].

  3. Egecan

    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!

  4. Nathan

    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…

    • Navjot Singh

      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]


      • 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:

        [php] <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; ?>[/php]

        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:

        [php]<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; ?> [/php]

        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.

        • Navjot Singh

          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]<?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; ?>[/php]

          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.

  5. Nathan

    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)) : ?>”>

  6. Nathan

    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)) : ?>

  7. Jared

    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.

    • Navjot Singh

      Can you be a bit more clear on what you want to achieve?

  8. Jose

    Thank you so much!!! Just what I was looking for.


  9. Thank you! This is a great tip and I will be using it from now on!


  10. 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!


  11. Excellent! This saved me a lot of time.
    Thanks!


  12. 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.

    • Navjot Singh

      Updated the post with do_shortcode() method.

        • Navjot Singh

          Applying the_content filter on the custom field. But that seemed an overkill for such a simple task.


  13. 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!

  14. rubin

    Disappeared after saving the admin interface.

  15. Brown

    Great! Thank you, been looking for this for a while.

  16. Sam H

    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


  17. Where and which file do I paste this code?! I can’t get it to work… Thanks in advance!

  18. Jenn

    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

    • Navjot Singh

      Pls publish the code in [php] [/php] tag…langcode was just used to show that u need to insert language’s code.


  19. 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.


  20. Hilmon

    ok … i give up :-)

  21. Hilmon

    Hi, the code in my reply didn’t seem to paste properly ….

    ID, “map”, true)); echo $map; ?>

  22. Hilmon

    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?? …..

  23. Joel

    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');

  24. Chris

    Whahoo!!

    Thanks so much. This is just what I needed. to get those wp-captions to show in my custom field template.

    thanks

  25. Maria

    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


  26. 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 :)

    • Navjot Singh

      Glad I could help. :)

  27. Asker

    Thanks a lot buddy.you save a lot time of me :)

    • Navjot Singh

      I am glad it has helped u in saving time. :)

  28. Adam

    In which file in WP do you put the code?
    Thanks

    • Navjot Singh

      In the file where you will be using the custom field’s value.


  29. 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!!!

    • Navjot Singh

      I am glad it helped you out. :)

Comments are closed.