I'm building a custom shortcode in Wordpress. Essentially what I want this to do is the following:
- Dynamically pull values from a group of Advanced Custom Fields
- Output those values into a javascript array, comma-separated
Where I'm getting stuck is that I can successfully run a loop over the group of fields and grab all the values and output them on a page. However, when I attempt to store them into a variable to be pulled into a javascript array ($v
is stored in the output
variable which is referenced in the urls
array), it only returns one of those values.
How can I get all the values from the foreach ( $value as $v)
to list inside of the urls
array, with comma separation? Any help would be appreciated :)
add_shortcode('training-player', 'training_player');
// Add shortcode for Diagnostic media playlist
function training_player() {
$author_id = get_the_author_meta('ID');
$fields = acf_get_fields(405);
if( $fields ) {
echo '<div id="player"></div>';
foreach( $fields as $field ) {
$value = get_field( $field['name'], 'user_'. $author_id );
if(!empty($value)) {
if ($value) {
foreach ( $value as $v) {
echo $v;
}
}
}
}
echo "
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.js'></script>
<script src='https://luwes.github.io/vimeowrap.js/vimeowrap.playlist.js'></script>
<script>
var output = '$v';
console.log(output)
vimeowrap('player').setup({
urls: [
output
],
plugins: {
'playlist':{}
}
});
</script>
";
}
}
$v_all = [];
beforeforeach( $fields as $field )
, replaceforeach ( $value as $v) { ... }
with$v_all[] = implode(',', $value);
, and aboveecho
add$v = implode(',', $v_all);
.array_walk( $value, function(&$item) { $item = '"' . $item . '"'; } );
added before$v_all[] = implode(',', $value);
.output
variable not being being parsed correctly when it's brought into the JSON.