Proper Shortcode Usage in Template File

I have a two part shortcode, with opening and closing shortcodes, in a template file. See example below.

echo do_shortcode('[myshortcode][/myshortcode]');

I see from the Codex that I can do this:

Read More
echo do_shortcode('[myshortcode]' . '<div class="anyHTML">Any Text</div>' . '[/myshortcode]');

Is it possible/correct or will it work to do this instead?

echo do_shortcode('[myshortcode]');
    $somePHP = possibleWordPressLoopCodeOrOtherPHP;
echo do_shortcode('[/myshortcode]');

I’m wondering about this to see if I can include PHP inside a two part shortcode, possibly a WordPress loop for a CPT or even other PHP code.

Related posts

Leave a Reply

1 comment

  1. You need to have the whole shortcode block inside the do_shortcode function. So you could do something like this:

    $text = some_code_or_function_that_returns_text_your_shortcode_can_act_on();
    $sc_string = sprintf("[myshortcode]n%sn[/myshortcode]", $text);
    echo do_shortcode($sc_string);