2016-03-07 65 views
1

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

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; 
    }); 
}); 
+0

什么“imgUrl的”必须包含什么价值? –

+0

它必须像我上传图片并点击插入按钮时,图片链接应该去文本字段。 – Arshad

回答

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'); 

重要提示:

之前单击插入按钮。请确认图片网址显示在文本字段中。请参阅参数HTML截图

enter image description here