Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I try the The Jedi Master way

http://scribu.net/wordpress/optimal-script-loading.html

class My_Shortcode {
    static $add_script;

    static function init() {
        add_shortcode('myshortcode', array(__CLASS__, 'handle_shortcode'));

        add_action('init', array(__CLASS__, 'register_script'));
        add_action('wp_footer', array(__CLASS__, 'print_script'));
    }

    static function handle_shortcode($atts) {
        self::$add_script = true;

        // actual shortcode handling here
    }

    static function register_script() {
        wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
    }

    static function print_script() {
        if ( ! self::$add_script )
            return;

        wp_print_scripts('my-script');
    }
}

My_Shortcode::init();

when [myshortcode] in widget, the js file is loaded.

But when [myshortcode] in a post , the js file is not loaded.

Could you tell me the problem ?

share|improve this question
1  
Post your code please. –  s_ha_dum Jan 4 at 15:21
    
i have updated code –  Quang Trung Nguyen Jan 5 at 3:51
    
Seems to work when I try it. –  s_ha_dum Jan 5 at 4:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.