我在wp小部件中上传图像时遇到问题。在下面的functions.php自定义小部件中的图像上传WP
class about_me extends WP_Widget{
public function __construct(){
parent::__construct('about_me', 'About Me', array(
'description' => 'This is all about me'
));
}
public function form($instance){
$title = $instance['title'];
$desc = $instance['desc'];
$photo = $instance['photo'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Your Name: </label></p>
<p>
<input class="widefat" type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo $title ?>">
</p>
<p><label for="<?php echo $this->get_field_id('desc'); ?>">Your Desc: </label></p>
<p>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('desc'); ?>" name="<?php echo $this->get_field_name('desc'); ?>"><?php echo $desc ;?></textarea>
</p>
<p><label for="<?php echo $this->get_field_id('photo'); ?>">Photo </label></p>
<p>
<input class="widefat image1" type="text" name="<?php echo $this->get_field_name('photo'); ?>" id="<?php echo $this->get_field_id('photo'); ?>" value="<?php echo $photo ;?>">
</p>
<p>
<button class="image_upload1 widefat">Select Image</button>
</p>
<?php
}
public function widget ($args, $instance){
$title = $instance['title'];
$desc = $instance['desc'];
echo $args['before_widget'].$args['before_title'].$title.$args['after_title']."<div class=\"textwidget\">".$desc."</div>".$args['after_widget'];
}
} // class about_me extends WP_Widget END
add_action('widgets_init', function(){
register_widget('about_me');
});
/*
photo upload option in widget
*/
function photo_upload_option($hook) {
if($hook != 'widgets.php')
return;
wp_enqueue_script('uploadphoto', get_template_directory_uri() . '/js/upload_image.js', array('jquery', 'media-upload', 'thickbox'), '1.0', 'true');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_style ('thickbox');
}
add_action('admin_enqueue_scripts', 'photo_upload_option');
现在,在码upload_image.js
jQuery(document).ready(function($){
$('button.image_upload1').click(function(){
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(){
var imagelink = jQuery('img').attr('src');
jQuery('.image1').val(imagelink);
tb_remove();
}
});
这给了我一个在文本字段中的gravatar链接。但我想在这里选择图像链接。
我尝试过,但它不工作。可能是Jquery有问题。当我从函数中删除“html”变量并在$('img',html)后面时,它会显示图片的链接,它是gravatar。 0.gravatar.com/avatar/...但是当把html变量显示为空白。如何解决此问题
你能否详细什么问题解释一下吗?你在哪里设置了'html'变量?我在您发布的代码中看不到这些内容。 –
嗨Ravi,当我设置HTML变量 window.send_to_editor = function(html){ var imagelink = jQuery('img',html).attr('src'); jQuery('。image1')。val(imagelink); tb_remove(); } 和选择图像后,我没有在文本字段中获得任何值。如果没有,那么得到gravatar链接。我想要我选择的文本字段中的相关图像链接。 –