Wordpress上传图片路径未显示在文本字段中

时间:2014-07-17 04:52:20

标签: php jquery wordpress wordpress-plugin

我正在使用插件和插件我想使用wordpress媒体上传,它成功上传图片。

我已经开始使用"媒体上传"和#34;厚盒"文件

add_action('admin_enqueue_scripts', 'wp_lsp_scripts');
function wp_lsp_scripts(){
    wp_register_style('lsp_admin_css', plugin_dir_url(__FILE__).'includes/css/lsp_style.css');
    wp_enqueue_style('lsp_admin_css');

    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('cp-script-handle', plugin_dir_url(__FILE__).'includes/js/lsp_custom.js', array( 'wp-color-picker' ), false, true);

    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');

    wp_enqueue_style('thickbox');
}

但问题是图像的路径没有显示在文本字段中

这是我的代码

upload_image.php:

<form action="" method="post" enctype="multipart/form-data">
    <table width="100%" border="0" cellspacing="4" cellpadding="0">
        <tr>
           <td width="22%">
               <label>
                    Upload New Image
               </label>
           </td>
           <td width="56%">
               <input type="text" size="50" name="lsp_slide_image" id="lsp_slide_image" class="lsp_upload_url" />
           </td>
           <td width="22%">
               <input id="lsp_upload_button" class="lsp_upload_button" type="button" name="upload_button" value="<?php _e('Upload Images','lsp'); ?>">
           </td>
        </tr>
        <tr>
           <td>&nbsp;</td>
           <td>&nbsp;</td>
           <td>
               <input type="submit" name="lsp_save_images" value="Save Image">
           </td>
        </tr>
    </table>
</form>

custome.js:

jQuery(document).ready(function() {

    jQuery('.lsp_upload_button').click(function() {
        targetfield = jQuery(this).prev('.lsp_upload_url');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });

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

});

任何人都请帮忙。

1 个答案:

答案 0 :(得分:0)

好的,我弄错了

以下是解决方案:

首先,我将颜色选择器和媒体上传文件分开

add_action('admin_enqueue_scripts', 'wp_lsp_scripts');
function wp_lsp_scripts(){
    wp_register_style('lsp_admin_css', plugin_dir_url(__FILE__).'includes/css/lsp_style.css');
    wp_enqueue_style('lsp_admin_css');

    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('cp-script-handle', plugin_dir_url(__FILE__).'includes/js/lsp_colorpicker.js', array( 'wp-color-picker' ), false, true);

    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');

    wp_enqueue_style('thickbox');

    wp_register_script('lsp_media_box', plugin_dir_url(__FILE__).'includes/js/lsp_media_box.js');
    wp_enqueue_script('lsp_media_box');
}

不改变&#34; upload_image.php&#34;文件,我制作和编辑代码:

jQuery(document).ready(function() {

    jQuery('.lsp_upload_button').click(function() {
        targetfield = jQuery(this).parents().find('.lsp_upload_url');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });

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

});

首先我使用此代码

targetfield = jQuery(this).prev('.lsp_upload_url');

并在html中我将输入字段放在一个<td>中,并将上传按钮放在另一个<td>中 所以.prev无效。

如果我将输入字段和上传按钮放在同一<td>我之前的代码中完美地工作