Support » Fixing WordPress » Allowing shortcode in custom fields

  • Hi there,

    I’ve been scratching my head on this one for a few hours and I don’t seem to be finding the answer.

    I’m wondering whether it’s possible to allow shortcode to be used in a custom field on a post.

    I’ve worked out that I can allow shortcodes in the_excerpt by using:

    add_filter('the_excerpt', 'do_shortcode');

    As a result of that I’ve gone on a long search for the filter that would cover the_meta or even a meta value with a specific meta key.

    I’ve read through the filter reference multiple times as well as trying to find the answer in Adam Brown’s excellent list of hooks and filters, but can’t seem to see anything that would act as a filter for custom fields. So yeah, I’m feeling a little lost.

    Anyone got an idea?



Viewing 11 replies - 1 through 11 (of 11 total)
  • I can’t find a hook/filter construct to handle custom fields, but I did this (below) and it worked, the disadvantage is that it is in the theme, so if you change the theme it will not work, but this is kind of a normal situation when using custom fields anyway, so I guess it’s ok.

    in your theme file do

    $values = get_post_custom_values(‘your custom field key’);
    $shortcode_output = do_shortcode($values[0]);
    print $shortcode_output;

    we use it on this site so through the admin they can put the description of the animations in the normal post box, and the shortcode for wordtube in a custom field box underneath.

    Hey @zz_james

    Thanks for the tip! I’ll definitely be checking that out 🙂

    As soon as I read the domain name I thought it was about this weird album I have and then realised that’s “The World Of Arthur Russell” – it’s a pretty strange record 😉

    Back on topic though, that’s a sweet way of sorting it out. I’m pretty happy to do stuff like that. I’ll have a think about turning that into a wee function 🙂

    This worked beautifully in 2.7 – I’m not having any luck with 2.8 beta 2…

    any thoughts?

    – opps – plugin conflict – this does work with 2.8 beta2

    I too would like to use the metadata to essentially divide each post into description/media. I’m trying to call up Audioplayer through the [audio:audiofile] shortcode inserted in a custom field.

    Where should I insert the code above into my theme? I’ve added it to the bottom of functions.php and I still see the text of the shortcode on the site, instead of the audio player. Should the code be elsewhere?




    Johnlago >

    any luck? I am trying to force Audioplayer to display on search results etc. to no avail.

    Assuming you just want to target a single value, you could just do this inside the loop.

    <?php echo ( do_shortcode( get_post_meta( $post->ID , 'abc' , true ) ) ); ?>

    If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array…

    <?php $values = do_shortcode( get_post_meta( $post->ID , 'abc' , false ) ); ?>
    <?php if($values && is_array($values)) : ?>
    <?php foreach( $values as $meta) : ?>
    <p><?php echo $meta ?></p>
    <?php endforeach; ?>
    <?php endif; ?>

    Purely an example, but it will work for your custom fields with either of the above, as long as you use it inside the loop (so $post->ID has a valid value).



    I’m too using AudioPlayer, and unfortunately

    <?php echo ( do_shortcode( get_post_meta( $post->ID , 'abc' , true ) ) ); ?>

    Didn’t allow me to use the following in a custom field:

    [audio: mysong.mp3]

    Although the plugin and that shortcode function perfectly within the_content.

    Did you change abc to the name of your custom field, i see you’ve left it in the code you’ve posted..



    Aye, I did.

    I tried the same with that plugin, same problem.

    If i knew the answer i’d offer it up, but i’m puzzled.. the only difference since testing is the theme i’m using.

    Previously i tested before posting, and both approaches worked (single and multiple values), now neither work, well at least not using the audio player plugin and the current theme.

    If i find the cause, i’ll post back. In honesty i have no need for such a piece of code, so i’ve only spent 5 minutes on it, but i’m sure somewhere down the line i may encounter further issues with this theme not executing code, if that’s the case i’ll let you know how i solve it.

    It may be worth testing the code in the default theme, just to see if the problem replicates or not… (my testing was in the Evolution (worked) theme and Thematic(not working)..)

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Allowing shortcode in custom fields’ is closed to new replies.