Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how to use an already existing shortcode via php in page template WordPress


here's my code:
global $post; // required to pull get_post_meta stuff

$abc = '
MLS#: ' . get_post_meta($post->ID, 'wpcf-mls', true) . '
Email: ' . get_post_meta($post->ID, 'wpcf-agent-email', true) . '

$xyz = '

[pl_tabtitlesection type="tabs"]
[pl_tabtitle active="yes" number="1"]Video[/pl_tabtitle]
[pl_tabtitle number="2"]Photos[/pl_tabtitle]
[pl_tabtitle number="3"]Details[/pl_tabtitle]
[pl_tabtitle number="4"]Contact[/pl_tabtitle]
[pl_tabtitle number="5"]Embed & Share[/pl_tabtitle]

[pl_tabcontent active="yes" number="1"]
video here
[pl_tabcontent number="2"]
[pl_tabcontent number="3"]
' . $abc . '
[pl_tabcontent number="4"]
contact-form to="[email protected]"
[pl_tabcontent number="5"]


echo $xyz;

which outputs this: (i.e. just plain text)

If I keep the same code but echo this instead:
echo wpautop( $xyz);
then the output looks like this: (looks nicer on page but isn't actually doing the shortcode)

What add_filter or action or whatever can I use to make $xyz render the shortcode instead of just output plain text to the page.
FYI: there is nothing in the WYSIWYG / HTML content area -- everything's being done via php to make a page template. FYI: When I tried echo apply_filters('the_content', $xyz); it just displayed the content area (i.e. blank / empty) and seemed to ignore $xyz 's content.

Thank you.

Answers (3)


enodekciw answers:

<?php $somevar = 'hello world'; ?>
<?php echo do_shortcode('[dummy string=' . $somevar . ']'); ?>

For your specific issue, use:

<?php $xyz = '[shortcode][anothershortcode]'; ?>
<?php echo do_shortcode( $xyz ); ?>

Clifford P comments:

for my specific issue, in my code above, I already set $xyz = 'first half of shortcode' . $abc . 'finish shortcode'
therefore, I just tried echo do_shortcode ( $xyz ); and nothing displayed on the page at all.

I'm looking for a format to do this:
start shortcode
interrupt shortcode with php logic / values
continue shortcode
more custom php logic / values
finish shortcode

So that I can "smart insert" values into a shortcode -- of course the end result will be like the screenshot I had above -- just need it to actually turn into a shortcode not be plain text.

Clifford P comments:

[[LINK href=""]][[/LINK]] is plain text. I want that plain text to be evaluated / rendered as a shortcode.

Clifford P comments:

um, now do_shortcode ($xyz); does work. must have been browser cache or something. that's the fix i needed, just that simple do_shortcode but didn't realize I could do it without '[a shortcode]' -- very cool.

thank you very much! :-)


Arnav Joy answers:

you have to use do_shortcode function , see

Clifford P comments:

I know that works, but then how do I get php code in the middle of it, like $abc ?
That's the workaround I'm looking for.

Arnav Joy comments:

can you show any example you want to use with shortcode ?

Clifford P comments:

Something like this:
Shortcodes here:


Navjot Singh answers:

What I would suggest is that instead of using so many shortcodes in your template file, you can use [[LINK href=""]]Get Custom Field Values[[/LINK]] plugin to get the value of your custom fields via shortcodes and then display them on a page via the editor. Would be a lot easier.

Clifford P comments:

That plugin looks useful. However, I have some other php things in mind that I want to put in between here; pulling a custom field is just one of the things. If I can get the code working this way, I can expand upon it.

Navjot Singh comments:

In that case, I would suggest you to parse each shortcode individually instead of nesting them with other code under a variable. Nesting of Shortcodes is something which doesn't work properly. There is already a bug in WordPress which hasn't been fixed since 2.8 series about that. ( )

Clifford P comments:

but the enclosing shortcode is what puts the other shortcode content into tabs...
plus, i don't see how requiring a space between brackets of shortcodes is relevant.