【问题标题】:How to access my uploaded images via Wordpress Media Uploader?如何通过 Wordpress Media Uploader 访问我上传的图片?
【发布时间】:2013-06-12 16:06:34
【问题描述】:

这是我第一次尝试在我的主题中使用 Wordpress Media Uploader,但我在访问我上传的图片时遇到了困难。
问题是:我可以上传我的图片,但我不知道如何检索它们

例如,当我点击我的上传按钮时,它会上传我的图片,但我的 textfield 是空白的(它应该显示我上传图片的路径)所以我可以像这样在我的主题中使用它:
<?php echo $options['body_background'];?>

我在 options-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
有人可以帮助我访问我上传的图片吗?

【问题讨论】:

标签: php jquery wordpress


【解决方案1】:

尝试将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>';
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2010-10-22
    相关资源
    最近更新 更多