How To Use A Shortcode Test In PHP Templates Using do_shortcode()

Knowledge Base > Premium > Running Tests > How To Use A Shortcode Test In PHP Templates Using do_shortcode()

in PremiumRunning Tests

When working with Shortcodes in PHP templates you need to utilise the do_shortcode() function.

The do_shortcode() function however expects that you will pass the entire shortcode, including the content and closing shortcode all at once.

This makes it a bit tricky when you have a lot of content going in between your split test shortcodes. The best method is to use Output Buffering.

Here is an example of how Output Buffering, Simple Page Tester shortcode tests and WordPress’s do_shortcode() function place nicely together:

<?php ob_start(); ?>
<p>This is the master content</p>
<?php
$masterContent = ob_get_contents();
ob_end_clean();
?>

<?php ob_start(); ?>
<p>This is the variation content</p>
<?php
$variationContent = ob_get_contents();
ob_end_clean();
?>

<?php echo do_shortcode( '[spt_split_test id=1234 variation="master"]' . $masterContent . '[/spt_split_test]' ); ?>
<?php echo do_shortcode( '[spt_split_test id=1234 variation="variation"]' . $variationContent . '[/spt_split_test]' ); ?>

0