Calling wp_enqueue_media() in a custom theme widget on WordPress 3.5.x cause js error

Simone Conti picture Simone Conti · Jan 30, 2013 · Viewed 8.2k times · Source

I am writing a custom widget for my own WordPress theme.

From WordPress 3.5 there is a new Media Uploader instead of the old ThickBox.

My widget used to work fine on WordPress versions older than 3.5, but now the new media uploader prevent the old working behavior.

I added a check in the costructor for the presence of wp_enqueue_media function:

if( function_exists( 'wp_enqueue_media' ) ) {
    wp_enqueue_media();
}

but when this part of cose is executed javascript throw an error in the console stopping Js engine:

Uncaught TypeError: Cannot read property 'id' of undefined    load-scripts.php:69

I removed all the widget code and reduced it to bare bones... the error is caused by wp_enqueue_media() calls, but I cannot get my head around why and how to fix it.

I also read Wordpress 3.5 custom media upload for your theme options, but there is no mention to this issue

Can anyone point me in the right direction? Is there any documentation available for the the WordPress 3.5 Media Uploader?

Answer

Filipe Pereira picture Filipe Pereira · Jun 16, 2014

It's too late for you now, but might be helpful for other people. I managed to make it work using

add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );

Hope it helps!