Ask your WordPress questions! Pay money and get answers fast! (more info)

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

  • SOLVED

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) . '
blah
';

$xyz = '
[pl_raw]
[pl_tabs]

[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_tabtitlesection]

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

[/pl_tabs]
[/pl_raw]
';

echo $xyz;


which outputs this: http://screencast.com/t/wRfmBt5h26 (i.e. just plain text)

If I keep the same code but echo this instead:
echo wpautop( $xyz);
then the output looks like this: http://screencast.com/t/f7zxWs47os6z (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)

2012-10-22

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="http://screencast.com/t/wRfmBt5h26"]]http://screencast.com/t/wRfmBt5h26[[/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! :-)

2012-10-22

Arnav Joy answers:

you have to use do_shortcode function , see

http://codex.wordpress.org/Function_Reference/do_shortcode


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


Arnav Joy comments:

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


Clifford P comments:

Something like this: http://www.pagelinestheme.com/cliff
Shortcodes here: http://demo.pagelin.co.in/framework/tools/

2012-10-22

Navjot Singh answers:

What I would suggest is that instead of using so many shortcodes in your template file, you can use [[LINK href="http://wordpress.org/extend/plugins/get-custom-field-values/"]]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. ( http://core.trac.wordpress.org/ticket/10082 )


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.