我想创建一个用于上传图片或从媒体中选择图片的小部件,因此用户可以选择图片或选择一个,我试过这个
小工具页面
class wd_aboutus extends WP_Widget
{
/**
* Constructor
**/
public function __construct()
{
$widget_ops = array(
'classname' => 'wd_aboutus',
'description' => 'Display About us data with image'
);
parent::__construct( 'wd_aboutus', 'WD - About Us', $widget_ops );
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', get_template_directory_uri() . '/js/upload-media.js');
}
/**
* Add the styles for the upload media box
*/
public function upload_styles()
{
wp_enqueue_style('thickbox');
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it's output
**/
public function widget( $args, $instance )
{
// Add any html to output the image in the $instance array
}
/**
* 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 )
{
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @param array An array of the current settings for this widget
* @return void
**/
public function form( $instance )
{
$title = __('Widget Image');
if(isset($instance['title']))
{
$title = $instance['title'];
}
$image = '';
if(isset($instance['image']))
{
$image = $instance['image'];
}
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></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( 'Image:' ); ?></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="Upload Image" />
</p>
<?php
}
}
这是我创建的小部件页面,我创建了一个像
的js文件上传媒体
jQuery(document).ready(function($) {
var image_field;
jQuery(function($){
$(document).on('click', 'input.upload_image_button', function(evt){
image_field = $(this).siblings('.img');
tb_show('', 'media-upload.php?type=image&TB_iframe=1');
return false;
});
window.send_to_editor = function(html) {
imgurl = $('img', html).attr('src');
image_field.val(imgurl);
tb_remove();
}
});
});
它工作正常,它确实显示了上传或选择在wordpress中使用的图像的框,但是它显示在页面的按钮中,并且它没有正确显示并且无法使用
我不知道发生了什么所以任何人都可以提供帮助?
顺便说一下,如果这个信息很重要,这是我创建的主题而不是wordpress的主题
答案 好的我通过将我的js文件更改为
来修复jQuery(document).ready( function(){
function media_upload( button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
jQuery('body').on('click',button_class, function(e) {
var button_id ='#'+jQuery(this).attr('id');
/* console.log(button_id); */
var self = jQuery(button_id);
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = jQuery(button_id);
var id = button.attr('id').replace('_button', '');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
jQuery('.custom_media_id').val(attachment.id);
jQuery('.custom_media_url').val(attachment.url);
jQuery('.custom_media_image').attr('src',attachment.url).css('display','block');
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
media_upload( '.upload_image_button');
});
但我仍然想要选择图像的名称,但它在我的文本字段中:)
所以我需要你的帮助
答案 0 :(得分:0)
<input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat img" type="text" size="36" value="<?php echo esc_url( $image ); ?>" />
请在输入类中添加“img”...
image_field = $(this).siblings('.img');