0

I'm trying to create a custom shortcode that uses another shortcode for a couple audio players on a Grid displaying a custom post type using Visual Composer.

The audio player that I would like to use is the Compact Audio Player plugin which uses a shortcode of [sc_embed_player fileurl="URL OF THE MP3 FILE"].

What I want to do is create 2 custom shortcodes that inject the correct mp3 url into the fileurl=" " path when the grid loads. I'm using advanced custom fields and the code that I am using for one shortcode in my functions.php now is (I plan on duplicating when I can get this one to work for the other shortcode):

function get_generic_demo() {
$demo = get_field( 'generic_demo');
echo do_shortcode('[sc_embed_player fileurl="'.$demo['url'].'"]');
}
add_shortcode( 'generic_demo', 'get_generic_demo');

However, it gives me an error saying I didn't input a valid url when the grid displays when my custom shortcode is entered.

I've tried both setting the return from ACF to file url and the array (as used above).

I've also tried:

$demo = get_field( 'generic_demo', $post->ID);

Any help would be much appreciated

  • Are you able to return ANY data from the $demo variable? – Ryan Jun 19 '17 at 23:14
  • not when using the variable. I am however able to get the correct data when coding in the template by using: <?php $generic_demo = get_field( 'generic_demo' ); ?> <?php if ( $generic_demo ) { ?> <audio controls src="<?php echo $generic_demo['url']; ?>"></audio> <?php } ?><br> – Kyle Jun 20 '17 at 0:24
0

Do not use echo in your shortcode. Use return instead.

function get_generic_demo() {
    //Get the field's value
    $demo = get_field( 'generic_demo');
    //Save the URL into a variable
    $url = $demo['url'];
    // Pass it to the other shortcode and return its value
    return do_shortcode('[sc_embed_player fileurl="'.$url.'"]');
}
add_shortcode( 'generic_demo', 'get_generic_demo');

Also make sure your fields contain the proper URL format. Dump the variable by using var_dump to verify it. This is most likely where your problem is coming from.

  • Thank you for your response. I dumped the variable by using var_dump($demo); which came back NULL as well as var_dump($url); which also returned null. Really bizarre because it works fine on the single page where its hard coded into the template but i'm having a really difficult time passing this darn URL into the shortcode... – Kyle Jun 19 '17 at 23:55
  • @Kyle If the $demo variable is NULL, then the problem is not the shortcode. You are probably retrieving the wrong field. You might wanna check that again. – Jack Johansson Jun 19 '17 at 23:58
  • I just double checked and the field_name is indeed generic_demo and the return value is set it File Array. I tried to add a second parameter of get_field( 'generic_demo', $attachment->ID ) but everthing i've tried is returning NULL. I've also tried to change the return on ACF to File URL and still doesn't work. Super bizarre since the field works fine on the single page where it's hardcoded – Kyle Jun 20 '17 at 0:06
  • @Kyle The question is where do you use this code? Does it have access to post ID? – Jack Johansson Jun 20 '17 at 0:53
  • I am using the custom [generic_demo] shortcode in a text field on the Grid Builder in Visual Composer. I want to be able to display an audio file in each post on the post grid that I'm building so the user can preview the audio file without going into the single post page for that post. Do I need to create a foreach loop inside that function so it pulls in each audio file when it renders the Featured Image, Excerpt, etc? Just thinking out loud... – Kyle Jun 20 '17 at 1:55
0

I'm not 100% positive if this will fix it, but try pulling the global post variable into your shortcode declaration function:

function get_generic_demo() {
    global $post;
    //Get the field's value
    $demo = get_field('generic_demo');
    //Save the URL into a variable
    $url = $demo['url'];
    // Pass it to the other shortcode and return its value
    return do_shortcode('[sc_embed_player fileurl="'.$url.'"]');
}
add_shortcode( 'generic_demo', 'get_generic_demo');

** RESPONSE EDIT - 2017-06-20 @ 11:45AM **

The issue appears to be that however Visual Composer is building the grid, the CPT IDs aren't being acknowledged. How is the rest of the data being set for each item entry (i.e. the title and description of each item)? Are you manually entering it into the grid or is it being automatically generated?

  • unfortunately that didn't do anything. Still getting the same error. – Kyle Jun 20 '17 at 16:27
  • I assume that the 'generic_demo' field is an ACF file field, correct? – Ryan Jun 20 '17 at 16:31
  • yes sir! It's working fine when i pull it in on a single.php page and its returning the url correctly. Just not when i'm trying to pass it through the fileurl of the Compact Audio shortcode – Kyle Jun 20 '17 at 16:44
  • So, the file is declared on a single post/cpt...are you using the shortcode on an alternative page/post or on that same page that you've attached the file to? – Ryan Jun 20 '17 at 16:51
  • I'm using the shortcode '[generic_demo]' in a text field on the Grid Builder in Visual Composer that is displaying a Grid of my CPT Posts – Kyle Jun 20 '17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.