Support » Plugin: Code Snippets » Struggling to use a global variable

  • Jason Chong

    (@dantianhealth)


    I am struggling to apply a global variable in a function using code snippets and wonder if there is something I am missing in the way your plugin works. My code is

    $dhURL = get_the_permalink();
    
    function facebookshare_function() {
        global $dhURL;
        $FBURL = 'https://www.facebook.com/sharer/sharer.php?u='.$dhURL;
        return $FBURL;
    }
    add_shortcode('facebookshare', 'facebookshare_function');

    The $dhURL is not being appended when running the shortcode. It works correctly if I run

    function facebookshare_function() {
        $dhURL = get_the_permalink();
        $FBURL = 'https://www.facebook.com/sharer/sharer.php?u='.$dhURL;
        return $FBURL;
    }

    However I wish to create other shortcodes in the same snippet (eg twitter) and use the same $dhURL variable.

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    I’m not entirely sure why this isn’t working, but I would recommend including $dhURL = get_the_permalink(); in each of the functions. Global variables are usually tricky to deal with.

Viewing 1 replies (of 1 total)
  • The topic ‘Struggling to use a global variable’ is closed to new replies.