Passing ACF Field To Shortcode

I’m currently creating a small plugin that just makes custom post types. I wanted to add a shortcode that easily places an audio player on the page. The URL of the MP3 can change per post, and I am using Advanced Custom Fields (ACF) to get the URL from the user. How can I pass the the URL field to the shortcode? Here is my shortcode function:

// Add A Shortcode for the MP3 Player
function sermon_audio_shortcode() {
    // Code
    $output = '<script>';
    $output .= 'audiojs.events.ready(function() {';
    $output .= 'var as = audiojs.createAll();';
    $output .= '});';
    $output .= '</script>';

    $mp3url = get_field('f1sermon_mp3_url');

    $output .= '<audio src="';
    $output .= 'echo $mp3url';
    $output .= '" preload="auto" />';

    return $output;
}
add_shortcode( 'sermon-audio', 'sermon_audio_shortcode' );

I tried just $output .= echo $mp3url; but that is incorrect as well. The shortcode works correctly but the URL is just “echo $mp3url” Any thoughts?

Related posts

1 comment

  1. I believe you just need to concatenate your string properly:

    $output .= $mp3url;
    

    Or combine the final three lines into one:

    $output .= '<audio src="' . $mp3url . '" preload="auto" />';
    

Comments are closed.