1

I think the title of my question says it all.

When creating a shortcode, I've seen some people do do_shortcode($content) instead of $content.

What is the difference?

Another thing, let's say I have a shortcode [hello]World[/hello]. Wouldn't returning do_shortcode($content) result in do_shortcode('World')?

From the codex, the examples given are as such, do_shortcode('[hello]')

So what will returning do_shortcode($content) interpret? (in the case of [hello])

Any practical examples or clear explanations would be greatly appreciated. Thanks.

2

This is useful if you don’t know if the $content contains unknown shortcodes.

Example

Your shortcode

add_shortcode( 'foo', 'shortcode_foo' );

function shortcode_foo(  $atts, $content = '' )
{
    return 'Foo!' . do_shortcode( $content );
}

Now your user might write something like this:

[foo][bar][/foo]

You have no idea what [bar] does or that it even exists. So you let WordPress handle that per do_shortcode(). If you don’t do that, [bar] will not be parsed as shortcode and showed as is instead.

  • Thanks for the quick answer! Am I safe to say it's best practice to return do_shortcode($content)? – Bobby Nov 24 '13 at 12:40
  • @Bobby That depends on what you want to do with the content. If you don’t care, then yes, you should. – fuxia Nov 24 '13 at 12:47

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.