Can anyone help me create a shortcode such as the one below by using a foreach loop?
The purpose of this shortcode is to generate a playlist, and uses opening and closing tags:
[zoomsounds id="favorites_playlist"] [/zoomsounds]
In between those tags, each song that is to be added to the playlist gets its own shortcode, like this:
[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]
So let's say a user has 2 songs in their playlist, the entire shortcode would look something like this:
[zoomsounds id="favorites_playlist"][zoomsounds_player config="favorites-playlist" source="http://www.test.com/sound/brobob.mp3" type="detect" songname="Song 1" init_player="off" play_target="footer"][zoomsounds_player config="favorites-playlist" source="http://www.test.com/sound/brobob2.mp3" type="detect" songname="Song 2" init_player="off" play_target="footer"][/zoomsounds]
As you can see, each song's shortcode comes one after another, enclosed in the start/close tags.
The easiest way I thought I could do this was to use a foreach loop to generate each song's shortcode. Here is the simple function I wrote.
function streamFavoritesPlaylist() {
$favs[] = get_user_favorites();
echo do_shortcode('[zoomsounds id="favorites_playlist"]');
foreach ($favs[0] as $fav) {
$title = get_the_title($fav);
$post = get_post($fav);
$post_slug = $post->post_name;
$source = '.../mp3/'.$post_slug.'.mp3';
$song_name = get_the_title($fav);
echo do_shortcode('[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]');
}
echo do_shortcode('[/zoomsounds]');
}
And this is the result that I get.
Clearly this is the wrong way to go about doing something like this. Not to mention, the closing tag did not take, and instead has been displayed in text. Perhaps this is why the entire thing is not working?
I should mention that by using the entire shortcode on it's own works fine. It's only when I try to build the shortcode in this manner does it not work.
How would you suggest I go about accomplishing something like this? Thanks a lot.
UPDATE: Thanks for everyone's input. This is what I came up with, which is similar to what @DACrosby posted, and @TheManiac suggested.
function streamFavoritesPlaylist() {
$favs[] = get_user_favorites();
$shortcode_full = "";
foreach ($favs[0] as $fav) {
$title = get_the_title($fav);
$post = get_post($fav);
$post_slug = $post->post_name;
$source = '.../mp3/'.$post_slug.'.mp3';
$song_name = get_the_title($fav);
$shortcode = '[zoomsounds_player config="favorites-playlist" source="'.$source.'" type="detect" songname="'.$title.'" init_player="off" play_target="footer"]';
$shortcode_full .= $shortcode;
}
return '[zoomsounds id="favorites_playlist"]'.$shortcode_full.'[/zoomsounds]';
}
And where I actually need the shortcode generated, I am using:
<?php echo do_shortcode(streamFavoritesPlaylist()); ?>
It's working perfectly. Thanks again, community.