小部件中的Wordpress媒体上传程序
我在WordPress主题中创建了一个小部件来显示图像。这个小部件工作到目前为止,我可以输入值并显示在前端 当我选择一个小部件并将其放入小部件区域时,媒体上载按钮不起作用。在WordPress屏幕的左窗格中,您可以选择要使用的小部件。当我取消隐藏(设置为display:none)这些小部件的代码,然后使用那里的上传按钮时,它就工作了。我猜是因为它是在一个被放入小部件区域之前调用的 我知道WordPress为每个小部件添加了唯一的参数,这样它们就不会相互冲突。因此,我想我需要为.js文件传递一个唯一的参数:#cc image upload file和#cc image upload file button。但我不知道该怎么做 那么谁能帮我解决这个问题呢 widget.php小部件中的Wordpress媒体上传程序,wordpress,widget,wordpress-theming,Wordpress,Widget,Wordpress Theming,我在WordPress主题中创建了一个小部件来显示图像。这个小部件工作到目前为止,我可以输入值并显示在前端 当我选择一个小部件并将其放入小部件区域时,媒体上载按钮不起作用。在WordPress屏幕的左窗格中,您可以选择要使用的小部件。当我取消隐藏(设置为display:none)这些小部件的代码,然后使用那里的上传按钮时,它就工作了。我猜是因为它是在一个被放入小部件区域之前调用的 我知道WordPress为每个小部件添加了唯一的参数,这样它们就不会相互冲突。因此,我想我需要为.js文件传递一个唯
// Image only
class cc_widget_image extends WP_Widget {
function cc_widget_image() {
$widget_ops = array('classname' => 'cc_widget_image', 'description' => __( 'Select and show an image.', 'cc_language' ) );
$this->WP_Widget('cc_widget_image', 'CC - ' . __( 'Image', 'cc_language' ), $widget_ops);
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$image = $instance['image'];
$checkbox = $instance['checkbox'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'cc_language'); ?>:
<input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image', 'cc_language'); ?>:
<input id="<?php echo $this->get_field_id('image'); ?>" class="widefat" type="text" name="<?php echo $this->get_field_name('image'); ?>" value="<?php echo $instance['image']; ?>" /></label>
</p>
<p>
<label for="cc-image-upload-file"><?php _e('Image', 'cc_language'); ?>:</label><br>
<label for="cc-image-upload-file">
<input type="text" id="cc-image-upload-file" class="widefat" name="<?php echo $this->get_field_name('image'); ?>" value="<?php echo $instance['image']; ?>" />
<input type="button" id="cc-image-upload-file-button" class="button" value="Upload file" />
<label for="cc-image-upload-file"><span class="description">Enter URL or upload file</span></label>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('checkbox'); ?>"><?php _e('Do not show title', 'cc_language'); ?></label>
<input id="<?php echo $this->get_field_id('checkbox'); ?>" type="checkbox" name="<?php echo $this->get_field_name('checkbox'); ?>" value="true" <?php checked( 'true', $checkbox ); ?> />
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['image'] = $new_instance['image'];
$instance['checkbox'] = strip_tags($new_instance['checkbox']);
return $instance;
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Image') : $instance['title'], $instance, $this->id_base);
echo $before_widget;
// display the widget title
if ( $instance['checkbox'] == 'true' ) {
} else {
if ( $title )
echo $before_title . $title . $after_title;
}
// display the widget content
echo the_post_thumbnail(array(220,200));
echo $instance['image'];
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("cc_widget_image");') );
谢谢你的答复。我找到了另一个使用不同脚本的解决方案,它可以正常工作:)
我在博客中使用Uploadcare.com小部件。非常简单的嵌入,好看,他们有一个免费的计划,这对我来说就足够了。这是插件我目前还不是JS高手,但看看这个答案:。看来这也可能是你的答案。。
jQuery(document).ready(function($){
var custom_uploader;
$('#cc-image-upload-file-button').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose File',
button: {
text: 'Choose File'
},
multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.live('select', function() {
attachment = custom_uploader.state().get('selection').first().toJSON();
$('#cc-image-upload-file').val(attachment.url);
});
//Open the uploader dialog
custom_uploader.open();
});
});
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( '.custom_media_upload');
});
jQuery(document).ready(function ($) {
var custom_uploader;
$('#cc-image-upload-file-button').click(function (e) {
e.preventDefault();
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Seleccione o adjunte archivo csv de importación',
button: {
text: 'Seleccione csv'
},
multiple: false
});
//When a file is selected, grab the URL and set it as the text field's value
custom_uploader.on('select', function () {
attachment = custom_uploader.state().get('selection').first().toJSON();
$('#cc-image-upload-file').val(attachment.url);
});
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Open the uploader dialog
custom_uploader.open();
});
});