I have a WordPress option that stores some data, like:


I'm displaying this option's value using echo get_option('my_option'));.

Of course the shortcodes doesn't work, and I'm wondering how to force them to do what they are supposed to do?

echo do_shortcode(get_option('my_option')); doesn't work either (I know this is an absolutely wrong approach, but I don't know any other way of displaying them).

  • Two things would be helpful here: (1) where are you calling do_shortcode? On the front end? (2) Where are you registering the shortcodes? – chrisguitarguy Nov 4 '11 at 3:18
  • @Christopher Davis, yes I called do_shortcode on the front-end, the shortcodes are registered within functions.php using add_shortcode and the options textarea is in admin panel. – Wordpressor Nov 4 '11 at 3:26
  • Same thing worked fine for me: gist.github.com/1338592 I suggest you make sure your shortcode callback functions are doing what you'd expect and also that you stick your add_shortcode calls inside a function hooked into init. – chrisguitarguy Nov 4 '11 at 3:33
  • @Christopher Davis, could you create a new answer so I will be able to accept? But to be honest I'm not sure what to do with the code provided, all shortcodes work like they should but I can't froce them to work even with this github snippet - what's wrong? – Wordpressor Nov 4 '11 at 17:13
  • Wait, after reseting my server do_shortcode(get_option('my_option')); works... :O – Wordpressor Nov 4 '11 at 17:47

Well, as Christopher Davis mentioned - do_shortcode works fine with extra stuff included, so I decided to reset my server and echo do_shortcode(get_option('my_option')); started to work perfectly.

So I guess the answer has always been here, execute shortcodes with do_shortcode.

  • sometimes it's better not to think about why things seem to randomly start working? – chrisguitarguy Nov 6 '11 at 4:47

Store only the Contents (or whatever you want the shortcode to be applied to), don't store the shortcode tags in the option. Then use get_option to fetch these contents and apply the shortcode to it:

//suppose you're storing content as $op = 'Lorem Ipsum', add_option('cont', $op).
$cont = get_option('cont');
echo do_shortcode('[my_shortcode]'.$cont.'[/my_shortcode]');

Try this. I think this'll do.

  • Unfortunately the option is not static, I mean, it could store really everything that user (admin) puts there, html tags, javascript code, over 200 shortcodes. – Wordpressor Nov 4 '11 at 2:52
  • Ohh... Then you might have to use some other trick, and it won't be simply doing do_shortcode. – Rutwick Gangurde Nov 4 '11 at 3:02
  • "...because the option value contains some other content as well." That's actually wildly incorrect. do_shortcode is the same function that processes the shortcode replacements for the post content and just about anything else that uses shortcodes. It works just fine with extra stuff included. – chrisguitarguy Nov 4 '11 at 3:15
  • You're right at that, I couldn't explain it well. But will 1 do_shortcode work for a couple of shortcodes spread randomly across the content? – Rutwick Gangurde Nov 4 '11 at 3:27
  • 1
    Yes, it will work no matter where the shortcodes are. Again: it's used to filter the content, which can have all kinds of shortcodes. – chrisguitarguy Nov 4 '11 at 3:31

This is how you embed a shortcode in your theme:

<?php echo do_shortcode('[shortcode option1="value1" option2="value2"]'); ?>

Hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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