I'm defining a function which can handle shortcodes for users wisiwyg made posts.
Using a function based on preg_replace_callback works great, but the returned replaced values prints before the initial string
This is the handler function
function shortcodify($string){
return preg_replace_callback('#\[\[(.*?)\]\]#', function($matches){
$parts = explode(':',$matches[1]);
$fnName = array_shift($parts);
if(function_exists($fnName)){
return call_user_func_array($fnName,$parts);
} else {
return $matches[0];
}
},$string);
}
This is the function which will replace the shortcode
function slider($tag){
//search $tag in DB
echo '<div>...'.$sliderContentFromDB.'...</div>';
}
The usage:
$postContent = "<h1>Super Slider</h1> [[slider:super-slider]] <p>Slider Description</p>";
shortcodify($postContent);
The expected result is:
<h1>Super Slider</h1>
<div>...super slider content...</div>
<p>Slider Description</p>
The actual result is:
<div>...super slider content...</div>
<h1>Super Slider</h1>
<p>Slider Description</p>
What could I be doing wrong?
slidercalled by the loop) the output comes out first.ob_start()andob_get_clean()then you can echo from the shortcodes but still capture the output in the callback