- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 WordPress 主题中创建了一个小部件来显示图像。到目前为止,小部件可以工作,我可以输入值并在前端显示这些值。
当我选择一个小部件并将其放入小部件区域时,媒体上传按钮不起作用。在 WordPress 屏幕的左 Pane 中,您可以选择要使用的小部件。当我取消隐藏(设置为 display:none)这些小部件的代码,然后使用那里的上传按钮时,它就可以工作了。我猜是因为它在落入小部件区域之前被调用。
我知道 WordPress 为每个小部件添加了独特的参数,因此它们不会相互冲突。所以,我想我需要为 .js 文件传递一个唯一的参数:#cc-image-upload-file 和 #cc-image-upload-file-button。但我不知道该怎么做。
那么谁能帮我解决这个问题?
小部件.php
// 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");') );
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();
});
});
最佳答案
感谢 Obmerk 的回复。我找到了另一个使用不同脚本的解决方案,它有效:)
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');
});
关于小部件中的 Wordpress 媒体 uploader ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466631/
我在项目中使用ngx-uploader实现文件上传。 但是当我上传多个文件时,它将文件数组分成多个请求。 我尝试使用 ng2-file-upload 但结果相同。 最佳答案 请参阅 GitHub 上的
我想要一个类似 this 的上传者但我想要一个进度条,并在完成后通过电子邮件向我发送通知,就像 yousendit 那样。 任何开源的东西都会很酷。 最佳答案 Uploadify允许有进度条。至于电子
我正在尝试编写一个Python脚本,可以将图片和pdf上传到WordPress。我希望图像上传到文件夹‘/wp-Content/Uploads/’,将pdf文件上传到文件夹‘/wp-Content/U
开发自定义 portlet 以在 Liferay 6.2 中上传多个文件。 在以下位置的文档库 Portlet 中浏览 Liferay 源代码时找到 Liferay.Upload 组件: https:
我正在尝试使用 HTML5 制作一个带有进度表的文件 uploader 。这是我的代码: Test Progress Meter function submit
当我选择一些图像并放入 WordPress 文件 uploader 时,该组的第一张图像此时似乎已正确上传,而其他图像则卡住且未得到处理。 但是,经过一段时间的等待,我停止了该进程,重新加载了浏览器选
我今天刚刚从 Cordova (PhoneGap) 1.5 升级到 1.9,突然我的 FileTransfer 参数停止发布。我可以说出来,因为我让服务器端调试了 $_POST 参数,它们现在是空白的
我已经在运行 RHEL7 的服务器上安装了 Mediawiki v1.24.1。 我已经将它安装在/var/www/foohelp/wiki 下。但是,当我尝试上传文件时,出现以下错误: [f3eae
在 Symfony2 中上传图片时,有没有办法调整图片大小? ImagineAvalancheBundle只允许在检索图像时将图像大小调整为缩略图,这对我来说并不是真正的性能。 此外,在发布数据时检索
我在网站上使用blueimp-file-upload,并且在使用webpack来组织我的js代码。 我从NPM安装了blueimp-file-upload和jquery.ui.widget npm i
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我需要获取上传的文件以将其推送到文件列表,但我无法做到这一点...我希望有人可以帮助我: UIkit.upload('.test-upload', { url: `/api/gridfs/${d
我基本上是一名 Java 开发人员,仅了解有关 Android 开发的基本信息。我开发了一个 Web 端点,它接受文件和一些其他参数。 java代码是 @RequestMapping(path = "
我正在使用 symfony.com 的食谱文章来实现图像的文件上传选项。 现在我想将其他图像加载到实体中。 默认的编辑策略是: 1.从数据库中取出 2. 注入(inject)表单 3.坚持 不知何故,
我需要处理通过(有和没有分块)上传到 Amazon S3 的每个文件的二进制数据。你知道 Fineuploader 中是否有我可以用来处理每个二进制 block /文件的函数/信号吗?: 例如: pr
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我读到 HTML5 规范引入了在上传表单中选择多个文件的功能。目前有哪些浏览器支持这个? Adobe AIR 是否支持它? 额外的问题:是否有利用此功能的 JavaScript 库? 最佳答案 即将发
我正在评估 Fine Uploader与其他各种选项相比,特别是 JQuery File Upload . 与依赖 Bootstrap 和 JQuery UI 的 JQuery File Upload
我正在尝试通过 Swift 2/Alamofire 将文件和参数上传到 Google 云端硬盘。在下面的代码中,我更改了以下行: "https://www.googleapis.com/upload/
我正在使用 Kendo UI Upload Control 并希望在同步模式下允许多个文件,但是当同时添加多个文件时,它们被组合在同一行项目中。有没有办法在组选择时将每个单独的文件作为自己的行项目?在
我是一名优秀的程序员,十分优秀!