how to call shortcode from php

In a custom template file, I need to call an affiliates shortcode if a condition is met:

if(some condition) echo do_shortcode('[affiliatestatschart]');

iow. I using the basic WordPress approach, but it does not work.

The white box for the chart is drawn, but the chart is not drawn.

How can I use affiliates shortcodes within a php page ?

( The chart is drawn correctly if I just add [affiliatestatschart]
to a post page in wp_admin. )

    Barry

    It depends at what point in the page you are calling it. It needs to be after the user has been authenticated by WP as it needs to grab that information.

    If it is within the WordPress loop, or on the sidebar / footer then you are usually ok.

    chrissfo
    • 26
    • #4,487

    Using BuddyPress,
    at the top of the page, I check to see if the user is logged-in and then get their ID.

    So the user has been authenticated.

    Then I create some data tables.

    Then I make the shortcode call near the bottom of the page....

    chrissfo
    • 26
    • #4,487

    Like so...

    <table id="stats-graph-container"><tr style="background: #FFF;"><td>
    
    <?php echo do_shortcode('[affiliatestatschart]');  ?>
    
    </td></tr></table>

    This works properly, if I paste it below the call to the custom template in a post page in wp_admin...

    [php] locate_template( array( 'custom/my_template.php' ), true ); [/php]
    
    <table id="stats-graph-container"><tr style="background: #FFF;"><td>
    [affiliatestatschart]
    </td></tr></table>

Join 366093 happy members

Get access to our entire collection of epic plugins, astonishing themes & genius support

Become a member