【问题标题】:jquery window.send_to_editor.. Can't insert File URL in the text fieldjquery window.send_to_editor .. 无法在文本字段中插入文件 URL
【发布时间】:2016-06-20 22:57:53
【问题描述】:

我创建了一个 Wordpress 文件上传器。就像当我单击添加媒体按钮时,我需要添加媒体上传器。但是当我选择图像时,它不会插入到字段中。我希望每当我选择图像并单击插入按钮时,图像 URL 应该被插入到文本编辑器中。

var image_field;
jQuery(function($){
  $(document).on('click', 'input.select-img', function(evt){
     image_field = $(this).siblings('.img');
     check_flag=1;
     tb_show('', 'media-upload.php?type=image&TB_iframe=true');
     window.send_to_editor = function(html) {  
       imgurl = $('img', html).attr('src');
       image_field.val(imgurl);
       tb_remove();
     }
    return false;
  });
});

【问题讨论】:

  • “imgurl”必须包含什么以及参数 html 中的值是什么?
  • 它必须像每当我上传图像并单击插入按钮时,图像链接应该转到文本字段。

标签: php jquery wordpress


【解决方案1】:

如果没有,请包含 jquery 库。

https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"

请包含下面的 jquery 代码,您将在其中添加 HTML 代码。

jQuery:

jQuery(document).ready(function() {
   jQuery('#upload_image_button').click(function() {
   formfield = jQuery('#upload_image').attr('name');
   tb_show('', 'media-upload.php?type=image&TB_iframe=true');

   window.send_to_editor = function(html) {
      imgurl = jQuery('img',html).attr('src');
      jQuery('#upload_image').val(imgurl);
      tb_remove();
   }
   return false;
});

HTML:

<tr valign="top">
  <td>Upload Menu Icon:</td>
  <td><label for="upload_image">
      <input id="upload_image" type="text" size="36" name="menu_icon" value="" required="" />
      <input id="upload_image_button" type="button" value="Upload Image" />
      <br />Enter an URL or upload an image from media.
      </label>
   </td>
</tr>

在您的主题 function.php 文件中包含以下 PHP 代码。

PHP:

function wp_gear_manager_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_enqueue_script('jquery');
}

function wp_gear_manager_admin_styles() {
    wp_enqueue_style('thickbox');
}

add_action('admin_print_scripts', 'wp_gear_manager_admin_scripts');
add_action('admin_print_styles', 'wp_gear_manager_admin_styles');

重要提示:

在单击插入按钮之前。请确认图像 URL 显示在文本字段中。请看截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多