Reputation: 1738
I need to get the content between a wordpress shortcode.
Like say [bla_blabla name="a"]Variation A[/bla_blabla]
should return Variation A
I thought of using regex to get it, but there is a shortcodes.php
file in wordpress which should already be capable of doing it? Could you guide me into the correct way of doing it?
I have currently used this code
<?php
function abtest_runner($input) {
//A bit confused if you wanted equal chances of selecting one or equal changes?
//This is for equal chances.
$size = count($input);
$select = rand(0,$size-1);
$temp=array();
$selection = $input[$select];
$temp = (explode("]",$selection));
$temp = (explode("[",$temp[1]));
echo $temp[0];
}
$input[0] = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$input[1] = '[bla_blabla name="b"]Variation B [/bla_blabla]';
abtest_runner($input);
?>
Upvotes: 1
Views: 1755
Reputation: 6345
If you have registered you shortcode in WordPress using the [add_shortcode][1]
function then, you can extract your shortcode content using the following method:
$pattern = get_shortcode_regex();
$content = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$matches = array();
preg_match("/$pattern/s", $content, $matches);
print_r($matches[5]);
$matches[5]
will contain your content.
Hope this helps.
Upvotes: 2