I hope you all having a great day. I'm developing a video player plugin with custom shortcodes and stuff but I'm having a problem with the shortcode render. Currently, my shortcode content is rendered with echo which works fine but it doesn't render inside the page content so I have to change it to return method somehow but it has foreach loops and if else statements inside it so I can't just do return '<ul> '.foreach($x as $s){}.' </ul> ';
and stuff, do you guys know a workaround for this? I've also tried to place everything inside a function and tried to render it inside return like this: return ' '.renderdata().' ';
which works but again it doesn't render inside the content.
Here's my shortcode function
function myshortcode( $atts = array() ) {
$data = ['some array with video data'];
foreach($data as $d){
echo "<div>$d</div>";
}
}
add_shortcode('someshortcode', 'myshortcode');
Any help is much appreciated, thank you.
EDIT: I was able to handle the problem with PHP Output Buffering, thanks to WordPress Codex.
Solution:
function myshortcode() {
ob_start();
// HTML here
return ob_get_clean();
}