Please vet this code and help me with how to declare '.($a['link']).' variable such that i can echo its content in single.php

function mp3_download_link_att($atts, $content = null) {
    $default = array(
        'link' => '#',
    global $post;
    $title = get_the_title($post->ID);
    $a = shortcode_atts($default, $atts);
    $content = do_shortcode($content);

    return ' <h2>Download '.$title.' Mp3 Audio</h2><p style="text-align: center;"><strong>Download Mp3 audio, listen and share this amazing song for free and stay happy.</strong></p><figure class="wp-block-audio"><audio class="audioPlayer" src="'.($a['link']).'" controls="controls"></audio></figure><center><p class="song-download"><a rel="nofollow" id="dlf" class="button download" href="'.($a['link']).'"><span class="fa fa-download"></span> DOWNLOAD '.$content.' MP3 <span class="fa fa-music"></span> </a></p></center>';
$_SESSION['myduration'] = ($a['link']);
add_shortcode('mp3download', 'mp3_download_link_att');

2 Answers 2


Declare $a as a global variable, just as you have with $post:

global $post, $a;

(Keep in mind, you may collide with other variables, since your var name is not really all that unique)

  • i did just that but this isn't still working. Dec 30, 2022 at 14:47
  • I tried to echo the content in single.php using <?php echo $aduration['link']; ?>, noting positive seems to be happening Dec 30, 2022 at 14:50
  • If $aduration was previously declared globally somewhere else in your code (like in a plugin file), then you need to globalize it when using it somewhere else (like in a template file), so it's clear that you're calling a global declared elsewhere. See my answer for example. Dec 31, 2022 at 6:40
  • "Order of operations" is key. If you're trying to pick this up in a template file directly (without being hooked to some action/filter), it is likely that you're accessing the global before it actually has a value placed in it (i.e. it's running in your template before the shortcode function has run). You probably need to be accessing it in a "the_content" filter that runs after shortcodes have run.
    – butlerblog
    Jan 1, 2023 at 13:43

Either declare your global variable outside of function scope (aka, global scope), or globalize it with the global keyword.

function foobar() {
  global $aduration;
  $aduration = shortcode_atts($default, $atts);

To access a global you declared elsewhere, use the global keyword again (globalize the variable), just like you do with global $post in your original code:

// Assuming `aduration` is declared global somewhere else, like shown above:
<?php global $aduration; echo $aduration['link']; ?>

In short, preceding any usage of your global variable with global $myglobal indicates that this is indeed a global variable - regardless of whether you're declaring it or accessing it. If you declare it inside a function in functions.php without the global keyword, it's nothing more than just another local variable, inside that function.

If declared properly as a global in functions.php, and want to access it somewhere else - such as in single.php - globalizing it effectively imports it, so php knows to check the global scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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