简体   繁体   English

在WordPress 3.5.x上的自定义主题小部件中调用wp_enqueue_media()会导致js错误

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

I am writing a custom widget for my own WordPress theme. 我正在为自己的WordPress主题编写自定义小部件。

From WordPress 3.5 there is a new Media Uploader instead of the old ThickBox. 从WordPress 3.5开始,有一个新的Media Uploader而不是旧的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. 我的小部件曾经在3.5以上的WordPress版本上正常工作,但现在新的媒体上传程序阻止了旧的工作行为。

I added a check in the costructor for the presence of wp_enqueue_media function: 我在costructor中添加了一个检查wp_enqueue_media函数的存在:

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: 但是当这部分cose被执行时,javascript会在控制台中抛出一个错误来停止Js引擎:

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. 我删除了所有的小部件代码,并将其简化为简单...错误是由wp_enqueue_media()调用引起的,但我无法理解为什么以及如何解决它。

I also read Wordpress 3.5 custom media upload for your theme options , but there is no mention to this issue 我还为您的主题选项阅读了Wordpress 3.5自定义媒体上传 ,但没有提到这个问题

Can anyone point me in the right direction? 谁能指出我正确的方向? Is there any documentation available for the the WordPress 3.5 Media Uploader? 有没有可用于WordPress 3.5媒体上传器的文档?

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. 您遇到的问题是因为您可能将自定义jquery放在标题中,而您没有注册wordpress jquery。 If multiple jquery are defined you will get that error. 如果定义了多个jquery,您将收到该错误。

My sugestion is you should either remove your jquery script or remove the one from wordpress 我的消息是你应该删除你的jquery脚本或从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. 我建议你使用jquery wordpress为你提供,如果没有,正确的排队方法是取消注册默认的一个寄存器你的jquery。 Just remove the comments from the remove_jquery function. 只需从remove_jquery函数中删除注释即可。

Also, the above code should go in functions.php 此外,上面的代码应该在functions.php中

Cheers. 干杯。

From codex [1], the function wp_enqueue_media( $args ) should be called from 'admin_equeue_scripts' action hook. 从codex [1]开始,应该从'admin_equeue_scripts'动作钩子调用函数wp_enqueue_media( $args ) 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]. [1]。 https://codex.wordpress.org/Function_Reference/wp_enqueue_media https://codex.wordpress.org/Function_Reference/wp_enqueue_media

To debug, you need to get the non-minified versions of the js sent to the browser. 要进行调试,您需要获取发送到浏览器的非缩小版本的js。 See the docs : 查看文档

SCRIPT_DEBUG SCRIPT_DEBUG

SCRIPT_DEBUG is a related constant that will force WordPress to use the "dev" versions of core CSS and Javascript files rather than the minified versions that are normally loaded. SCRIPT_DEBUG是一个相关的常量,它将强制WordPress使用核心CSS和Javascript文件的“dev”版本,而不是通常加载的缩小版本。 This is useful when you are testing modifications to any built-in .js or .css files. 当您测试对任何内置.js或.css文件的修改时,这非常有用。 Default is false. 默认值为false。

define('SCRIPT_DEBUG', true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM