2013-06-12 75 views
1

這是我第一次嘗試使用WordPress的媒體上傳我的主題裏面,但我有困難訪問我上傳的圖片
這裏的問題是:我可以上傳我的圖片,但我不知道如何檢索它們

例如,當我點擊我的上傳按鈕,它上傳我的形象,但我文本框是空白的(它應該顯示路徑我上傳的圖片),所以我可以用它在我的主題裏面,像這樣:
<?php echo $options['body_background'];?>如何通過Wordpress Media Uploader訪問我上傳的圖片?

我的功能裏面選項-page.php文件看起來是這樣的:

//Heading Text (works fine and i can echo value out...) 
function text_heading_setting(){ 
$options = get_option('plugin_options'); 
$get_options = $options['text_heading']; 
echo '<input type="text" name="plugin_options[text_heading]" value="'.$get_options.'" >'; 
} 

//Menu Image upload field (can upload image but can't echo its path) 
function body_background_setting(){ 
$options = get_option('plugin_options'); 
$get_options_menu = $options['menu_background']; 
echo '<div class="uploader"> 
    <input type="text" name="plugin_options[menu_background]" id="menu_image_bg" value="'.$get_options_menu.'" /> 
    <input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" /> 
    </div>'; 
    } 


    //Body Image upload field (can upload image but can't echo its path) 
    function body_background_setting(){ 
    $options = get_option('plugin_options'); 
    $get_options_body_bg = $options['body_background']; 
    echo '<div class="uploader"> 
    <input type="text" name="plugin_options[body_background]" id="menu_image_bg" value="'.$get_options_body_bg.'" /> 
    <input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" /> 
    </div>'; 
    } 

上傳JS看起來是這樣的:

jQuery(document).ready(function($){ 
var _custom_media = true, 
    _orig_send_attachment = wp.media.editor.send.attachment; 

$('.button').click(function(e) { 
var send_attachment_bkp = wp.media.editor.send.attachment; 
var button = $(this); 
var id = button.attr('id').replace('_button', ''); 
_custom_media = true; 
wp.media.editor.send.attachment = function(props, attachment){ 
    if (_custom_media) { 
    $("#"+id).val(attachment.url); 
    } else { 
    return _orig_send_attachment.apply(this, [props, attachment]); 
    }; 
} 

wp.media.editor.open(button); 
return false; 
}); 

$('.add_media').on('click', function(){ 
_custom_media = false; 
}); 
}); 

我的問題的屏幕截圖: http://vasinternetposao.com/wordpressdevelopment/imgupload.png
有人可以幫助我,讓我可以訪問我上傳的圖片嗎?

+0

採取的鏈接一看,希望這可以幫助您http://webomnizz.com/blog/php/how-to-use-wordpress-uploader-into - 您的主題/ –

回答

2

嘗試加入esc_url()的值

//Menu Image upload field (can upload image but can't echo its path) 
function body_background_setting(){ 
$options = get_option('plugin_options'); 
$get_options_menu = $options['menu_background']; 
echo '<div class="uploader"> 
    <input type="text" name="plugin_options[menu_background]" id="menu_image_bg" value="'.esc_url($get_options_menu).'" /> 
    <input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" /> 
    </div>'; 
    } 


    //Body Image upload field (can upload image but can't echo its path) 
    function body_background_setting(){ 
    $options = get_option('plugin_options'); 
    $get_options_body_bg = $options['body_background']; 
    echo '<div class="uploader"> 
    <input type="text" name="plugin_options[body_background]" id="menu_image_bg" value="'.esc_url($get_options_body_bg).'" /> 
    <input class="button" name="_unique_name_button" id="_unique_name_button" value="Upload" /> 
    </div>'; 
    }