我正在尝试在Wordpress小部件中上传媒体。 我用Google搜索了一下,发现了这个:
http://www.paulund.co.uk/add-upload-media-library-widgets
以该教程为基础,我编写了以下内容(大部分内容,复制+粘贴):
<?php
error_reporting(E_ALL);
/*
Plugin Name: Dulkre Widgets
Plugin URI: http://www.dulkre.com.ar
Description: Widgets para los laterales del sitio dulkre.com.ar
Version: 1.0
Author: Pablo Bertran
Author URI: http://www.pablobertran.com
*/
class InfoProducto extends WP_Widget {
function __construct(){
$params = array(
'description' => 'Informacion que se presenta al lateral de cada producto.',
'name' => 'InfoProducto',
);
parent::__construct('InfoProducto', '', $params );
add_action('admin_enqueue_scripts', array($this, 'upload_scripts'));
add_action('admin_enqueue_styles', array($this, 'upload_styles'));
}
/**
* Upload the Javascripts for the media uploader
*/
public function upload_scripts()
{
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('upload_media_widget', plugin_dir_url(__FILE__) . 'upload-media.js', array('jquery'));
}
/**
* Add the styles for the upload media box
*/
public function upload_styles()
{
wp_enqueue_style('thickbox');
}
public function form( $instance ){
$title = __('Widget Image');
if(isset($instance['title']))
{
$title = $instance['title'];
}
$image = '';
if(isset($instance['image']))
{
$image = $instance['image'];
}
/*
$image2 = '';
if(isset($instance['image2']))
{
$image2 = $instance['image2'];
}
$pdf = '';
if(isset($instance['pdf']))
{
$pdf = $instance['pdf'];
}*/
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Titulo:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Imagen 1:' ); ?></label>
<input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat" type="text" size="36" value="<?php echo esc_url( $image ); ?>" />
<input class="upload_image_button button button-primary" type="button" value="Cargar Imagen" />
</p>
<!-- <p>
<label for="<?php echo $this->get_field_name( 'pdf' ); ?>"><?php _e( 'PDF Informacion Nutricional:' ); ?></label>
<input name="<?php echo $this->get_field_name( 'pdf' ); ?>" id="<?php echo $this->get_field_id( 'pdf' ); ?>" class="widefat" type="text" size="36" value="<?php echo esc_url( $pdf ); ?>" />
<input class="upload_pdf_button button button-primary" type="button" value="Cargar PDF" />
</p>
<p>
<label for="<?php echo $this->get_field_name( 'image2' ); ?>"><?php _e( 'Imagen 2:' ); ?></label>
<input name="<?php echo $this->get_field_name( 'image2' ); ?>" id="<?php echo $this->get_field_id( 'image2' ); ?>" class="widefat" type="text" size="36" value="<?php echo esc_url( $image2 ); ?>" />
<input class="upload_image_button button button-primary" type="button" value="Cargar Imagen" />
</p> -->
<?php
}
public function widget( $args, $instance ){
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @param array An array of new settings as submitted by the admin
* @param array An array of the previous settings
* @return array The validated and (if necessary) amended settings
**/
public function update( $new_instance, $old_instance ) {
// update logic goes here
$updated_instance = $new_instance;
return $updated_instance;
}
}
add_action('widgets_init', function(){
register_widget( 'InfoProducto' );
});
?>
使用媒体库加载厚盒的javascript
jQuery(document).ready(function($) {
$(document).on("click", ".upload_image_button", function() {
jQuery.data(document.body, 'prevElement', $(this).prev());
window.send_to_editor = function(html) {
var imgurl = jQuery('img',html).attr('src');
var inputText = jQuery.data(document.body, 'prevElement');
if(inputText != undefined && inputText != '')
{
inputText.val(imgurl);
}
tb_remove();
};
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
});
问题是,当我点击Cargar Imagen按钮时,没有任何内容加载,并且没有控制台错误消息。 我知道我做错了什么?
提前感谢!