Shortcode return $content vs do_shortcode($content)

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.

Read More

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.

Related posts

1 comment

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

Comments are closed.