我正在开发一个小型WordPress小部件,并试图将其设置为用户可以上传图像的位置。到目前为止,正在加载必要的脚本,但当我单击“媒体库图像”按钮时,图像上载程序不会弹出。
这是我的javascript文件:
jQuery(document).ready(function($) {
var formfield = null;
$(\'#upload_image_button\').click(function() {
$(\'html\').addClass(\'Image\');
formfield = $(\'#wp-ss-image\').attr(\'name\');
tb_show(\'\', \'media-upload.php?type=image&TB_iframe=true\');
return false;
});
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
var fileurl;
if (formfield != null) {
fileurl = $(\'img\',html).attr(\'src\');
$(\'#wp-ss-image\').val(fileurl);
tb_remove();
$(\'html\').removeClass(\'Image\');
formfield = null;
} else {
window.original_send_to_editor(html);
}
};
});
以下是我调用脚本的函数:add_action(\'admin_print_scripts-widgets.php\', \'wp_ss_image_admin_scripts\');
function wp_ss_image_admin_scripts() {
wp_enqueue_script( \'wp-ss-image-upload\', plugins_url( \'/WP-Simple-Social/wp-simple-social-image.js\' ), array( \'jquery\',\'media-upload\',\'thickbox\' ) );
}
add_action(\'admin_print_styles-widgets.php\', \'wp_ss_admin_styles\');
function wp_ss_admin_styles() {
wp_enqueue_style( \'thickbox\' );
}
下面是显示输入框和图像上传按钮的html:<p>Upload Photo: <input id="wp-ss-image" class="widefat" type="text" size="75" name="<?php echo $this->get_field_name( \'image\' ); ?>" value="<?php echo esc_url( $image ); ?>" />
<input id="upload_image_button" type="button" value="Media Library Image" class="button-secondary" />
由于正在加载所有必需的文件/脚本,这可能是jQuery问题吗?如果有任何其他有帮助的信息,请让我知道。谢谢你的帮助。