2010-06-08 15 views
8

Sto sviluppando un plugin per wordpress e ho bisogno di avere un campo in cui posso caricare un'immagine. Dal momento che wordpress ha un uploader molto pratico sarebbe bello se potessi usarlo.come usare wordpress upload file/codice immagine nel mio plugin

qualcuno sa se è possibile?

Grazie

+2

http://www.webmaster-source.com/2010/01/08/using- the-wordpress-uploader-in-your-plugin-or-theme/ – Anonymous

+0

Ho scritto un tutorial su questo, come utilizzare l'uploader multimediale a discesa da Wordpress :) http://www.cedricve.me/2012/03/ 31/use-the-built-in-wordpress-uploader-in-your-own-wp-plugin/Spero che questo possa aiutare te o qualcun altro. Buona giornata –

risposta

14

Se si desidera solo caricare un file, non è necessario l'uploader media. Una semplice forma è tutto ciò di cui hai bisogno.

Per chiamare l'uploader supporto avete bisogno di un link come questo:

<a onclick="return false;" title="Upload image" class="thickbox" id="add_image" href="media-upload.php?type=image&amp;TB_iframe=true&amp;width=640&amp;height=105">Upload Image</a> 

Avrai forse necessario aggiungere l'URL a media-upload.php per farlo lavorare.

+0

>>> Dovrai forse aggiungere l'URL a media-upload.php per farlo funzionare. come faccio? – vick

+0

Basta usare /Wp-admin/media-upload.php? Type = image & TB_iframe = true & width = 640 & height = 105 per l'attributo href che includerà i blog url e la cartella wp-admin erano i media-upload.php si trova. – 2ndkauboy

+0

Che cosa farà il tuo plug-in? Ho scritto personalmente 3 plugin. Forse il tuo plugin potrebbe essere utile per me. E lo sapevi che puoi votare una risposta E accettarla;) – 2ndkauboy

2

è possibile utilizzare wordpress file multimediale predefinito uploader utilizzando questo codice e semplicemente recuperare collegamento di immagine in jQuery

<label for="upload_image"> 
    <input id="upload_image" type="text" size="36" name="ad_image" value="http://" /> 
    <input id="upload_image_button" class="button" type="button" value="Upload Image" /> 
    <br />Enter a URL or upload an image 
</label> 

<?php 
add_action('admin_enqueue_scripts', 'my_admin_scripts'); 

function my_admin_scripts() { 
    if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') { 
     wp_enqueue_media(); 
     wp_register_script('my-admin-js', WP_PLUGIN_URL.'/my-plugin/my-admin.js', array('jquery')); 
     wp_enqueue_script('my-admin-js'); 
    } 
} 

?> 

<script> 
    jQuery(document).ready(function($){ 


    var custom_uploader; 


    $('#upload_image_button').click(function(e) { 

     e.preventDefault(); 

     //If the uploader object has already been created, reopen the dialog 
     if (custom_uploader) { 
      custom_uploader.open(); 
      return; 
     } 

     //Extend the wp.media object 
     custom_uploader = wp.media.frames.file_frame = wp.media({ 
      title: 'Choose Image', 
      button: { 
       text: 'Choose Image' 
      }, 
      multiple: true 
     }); 

     //When a file is selected, grab the URL and set it as the text field's value 
     custom_uploader.on('select', function() { 
      console.log(custom_uploader.state().get('selection').toJSON()); 
      attachment = custom_uploader.state().get('selection').first().toJSON(); 
      $('#upload_image').val(attachment.url); 
     }); 

     //Open the uploader dialog 
     custom_uploader.open(); 

    }); 


}); 
    </script> 
+0

aggiunto altre due righe e visualizzando l'immagine selezionata come miniatura. $ ('# upload_image_src'). Attr ('src', attachment.url); –

Problemi correlati