[英]Wordpress Media Uploader register & enqueue javascript correctly
I have the following function to open the Wordpress Media Uploader within the main php file of a Wordpress plugin. 我具有以下功能,可以在Wordpress插件的主php文件中打开Wordpress Media Uploader。 The problem is getting the javascript to fire:
问题是要触发javascript:
function media_uploader_enqueue() {
wp_enqueue_media();
wp_register_script( 'media-uploader', plugins_url( 'media-lib-uploader.js' , __FILE__ ), array( 'jquery', NULL, false ) );
wp_enqueue_script( 'media-uploader');
}
add_action( 'admin_enqueue_scripts', 'media_uploader_enqueue');
and the following in media-lib-uploader.js 以及media-lib-uploader.js中的以下内容
jQuery(document).ready(function($){
var mediaUploader;
$('#upload_image_button').click(function(e) {
e.preventDefault();
if (mediaUploader) {
mediaUploader.open();
return;
}
mediaUploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
}, multiple: false });
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
$('#background_image').val(attachment.url);
});
mediaUploader.open();
});
});
This is the html for the settings page: 这是设置页面的html:
<input type="text" id="background_image" name="background_image" value="<?php echo get_option('background_image'); ?>" /><input id="upload_image_button" class="button" type="button" value="Upload Image" />
I cannot find the error within the code. 我在代码内找不到错误。 I have even tried;
我什至尝试过;
wp_deregister_script
Have I registered and enqueued the javascript correctly? 我是否已正确注册和排队javascript? Or is there a problem with the javascript itself?
还是JavaScript本身有问题?
Fixed the issue once I defined this function ahead of all others in my main plugin php file. 解决了我在主插件php文件中先于所有其他函数定义此函数后解决的问题。 Simple!
简单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.