WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try the The Jedi Master way


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 )



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
Post your code please. – s_ha_dum Jan 4 '14 at 15:21
i have updated code – Quang Trung Nguyen Jan 5 '14 at 3:51
Seems to work when I try it. – s_ha_dum Jan 5 '14 at 4:02

Your Answer


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

Browse other questions tagged or ask your own question.