Why won’t WordPress load thickbox and media-upload scripts?

I’m working on a plugin that uses thickbox and media-upload to set some images. Neither will load using this code:

  function add_my_files() {
        echo 'happy happy happy';
        wp_register_style( 'adminstyles', plugins_url('/css/slider.css', __FILE__));
        wp_enqueue_style( 'adminstyles' );
        wp_enqueue_style('thickbox');

        wp_enqueue_script('media-upload');
        wp_enqueue_script('thickbox');

        wp_register_script('hdjs',plugins_url('/js/slider.js', __FILE__),array('media-upload','thickbox'),'',true);
        wp_enqueue_script('hdjs');
    }

add_action( 'admin_init', 'add_my_files' );

my css and js files load but not thickbox and media-upload.

Read More

Thanks

Related posts

Leave a Reply

1 comment

  1. The correct hook to include your asset files in WP is admin_enqueue_scripts:

    NOTE: I recommend you too use get_current_screen (see is_my_admin_screen() definition below) to just include your js/css files when you actually needed.

    add_action('admin_enqueue_scripts', 'add_my_files');
    
    function add_my_files()
    {
        /*
         * a good WP citizen only loads
         * their javascript/css where it is needed
         */
        if ( ! is_my_admin_screen()) // defined below
            return;
    
        wp_register_style('adminstyles', plugins_url('/css/slider.css', __FILE__));
        wp_enqueue_style('adminstyles');
        wp_enqueue_style('thickbox');
    
        wp_enqueue_script('media-upload');
        wp_enqueue_script('thickbox');
    
        wp_register_script('hdjs', plugins_url('/js/slider.js', __FILE__), array('media-upload', 'thickbox'), '', true);
        wp_enqueue_script('hdjs');
    }
    
    function is_my_admin_screen()
    {
        $screen = get_current_screen();
    
        if (is_object($screen) && $screen->id == 'my_plugin_page_id') // var_dump($screen->id); find your own id
            return true;
        else
            return false;
    }
    

    ref: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
    ref: http://codex.wordpress.org/Function_Reference/get_current_screen


    Besides hopefully you are using a class to wrap all your plugin or you will have worse problems than this.

    Please feedback. I am very interested in this issue because WP plugins puts food and beers on my table.