2014-04-04 157 views
5

我發展我的第一個WordPress插件上傳圖片。它只需要允許用戶更改自定義模板中的徽標並更改自定義模板中的配色方案。WordPress的:在管理選項頁

我做了一個管理選項頁,現在要增加一個字段,以允許用戶上傳圖片。我如何將圖片上傳到wp-content/uploads文件夾。到目前爲止,我有這個表內:

<td><input name="logo_image" type="file" id="logo_image" value="" /></td> 

這是正確的做法?如果是這樣,我如何將文件定向到正確的文件夾?不wordpress有它自己的方式處理文件上傳?

+0

這將創建上傳媒體目錄空,但不渲染默認的媒體彈出部分 –

回答

0

可以使用wordpress的

<?php wp_handle_upload($file, $overrides, $time); ?> 

這個內置的功能會自動將文件移動到文件夾上傳

或者

您可以編寫自己的PHP功能。

其他詳細信息可以在這裏找到 - >Wordpress File Upload

13

將此代碼添加到您的全球定製選項功能。

if(function_exists('wp_enqueue_media')){ 
    wp_enqueue_media(); 
}else{ 
    wp_enqueue_style('thickbox'); 
    wp_enqueue_script('media-upload'); 
    wp_enqueue_script('thickbox'); 
} 

<p><strong>Header Logo Image URL:</strong><br /> 
       <img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/> 
       <input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>"> 
       <a href="#" class="header_logo_upload">Upload</a> 

</p>  


<script> 
    jQuery(document).ready(function($) { 
     $('.header_logo_upload').click(function(e) { 
      e.preventDefault(); 

      var custom_uploader = wp.media({ 
       title: 'Custom Image', 
       button: { 
        text: 'Upload Image' 
       }, 
       multiple: false // Set this to true to allow multiple files to be selected 
      }) 
      .on('select', function() { 
       var attachment = custom_uploader.state().get('selection').first().toJSON(); 
       $('.header_logo').attr('src', attachment.url); 
       $('.header_logo_url').val(attachment.url); 

      }) 
      .open(); 
     }); 
    }); 
</script> 

More info

Media uploader in theme and plugin

enter image description here

+0

不知道在哪裏添加此...管理員菜單是一個PHP文件,我只是簡單地添加JS部分?? –

+0

這個取決於你的函數爲您的設置。如何從我的理解創造的外觀聽到 https://codex.wordpress.org/Creating_Options_Pages –

+0

- WordPress的建議使用定製API,而不是... https://developer.wordpress.org/themes/advanced-topics/customizer -API / –