0

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 ?

3
  • 1
    Post your code please.
    – s_ha_dum
    Commented Jan 4, 2014 at 15:21
  • i have updated code
    – Chameron
    Commented Jan 5, 2014 at 3:51
  • Seems to work when I try it.
    – s_ha_dum
    Commented Jan 5, 2014 at 4:02

0

Your Answer

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

Browse other questions tagged or ask your own question.