简体   繁体   中英

PHP Variable Scope Issue

I am writing a plugin for WordPress but I can't seem to get my flag going. It is driving me nuts! Here's the code without the class wrapper:

static $js_flag;

function init() 
{
    add_shortcode('jw_html5', array(__CLASS__, 'jw_html5_shortcode'));
    add_action('init', array(__CLASS__, 'jw_add_js' ));
}

public function jw_html5_shortcode( $atts, $content = null ) 
{

    self::$js_flag = true;
    $vid = '';

    extract( shortcode_atts( array(
        'src' => '/vids/video.mp4',
        'width' => 480,
        'height' => 320
        ), $atts ) );

    $vid = "<video id='player' src='$src' width='$width' height='$height' type='video/mp4'> </video>";

    return self::$js_flag;
    //return $vid;
}


public function jw_add_js()
{


    if(self::$js_flag)
    {
        wp_register_script('jw_player', plugins_url('scripts/player.php', __FILE__), array('jquery'), '.01', true);
        wp_register_script('jw_player_script', plugins_url('scripts/jquery.jwplayer.js', __FILE__), array('jquery'), '.01', false);
        wp_register_script('jw_playlist', plugins_url('scripts/jquery.playlist.js', __FILE__), array('jquery'), '.01', false);

        wp_enqueue_script('jw_player_script');
        wp_enqueue_script('jw_playlist');
        wp_enqueue_script('jquery');
        wp_enqueue_script('jw_player');
    }   


}

I've set the flag in the shortcode function but it's not passing it to the jw_add_js() function. It's sort of a bitch to debug in WP. Any advice would be super appreciated.

Update: I found the answer to this after messing around. I had to register my scripts and then store the handles in an array. I then pass the array with wp_print_scripts() method. Hope this will help someone!

I found the answer to this after messing around. I had to register my scripts and then store the handles in an array. I then pass the array with wp_print_scripts() method. Hope this will help someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM