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

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


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!


The problem you are experiencing is because you probably put your custom jquery in the header and you didn't registered wordpress jquery. If multiple jquery are defined you will get that error.

My sugestion is you should either remove your jquery script or remove the one from wordpress

function remove_jquery() {wp_deregister_script('jquery');//wp_register_script('jquery', ("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false);}if(!is_admin()){add_action('init', 'remove_jquery');}

I suggest you use the jquery wordpress provides you, if not, the proper way to enqueue it is to deregister the default one an register your jquery. Just remove the comments from the remove_jquery function.

Also, the above code should go in functions.php

Cheers.


From codex [1], the function wp_enqueue_media( $args ) should be called from 'admin_equeue_scripts' action hook. or later.

Example:

function enqueue_media() {    if( function_exists( 'wp_enqueue_media' ) ) {        wp_enqueue_media();    }}add_action('admin_enqueue_scripts', 'enqueue_media');

Hope it helped.

[1]. https://codex.wordpress.org/Function_Reference/wp_enqueue_media