2016-03-07 60 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