Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the media uploader in wordpress plugin [duplicate]

I read out some of the tutorial for how to integrate the media uploader in wordpress plugins. I do the media uploader based on the tutorial. http://wordpress.org/support/topic/howto-integrate-the-media-library-into-a-plugin?replies=4 I do this and it perfectly working. When i tried the same script again for multiple times of media uploader, Here is the fiddle i tried simply changed the id of the particular text field. http://jsfiddle.net/UrXPe/1/ Still when i click the upload all is to be to done perfect. only thing if i click insert into post it url of the image appear in the second browse field. Here is the screenshot what i face exactly. enter image description here

When i click the first upload field (uploading process are success) after insert into post that corresponding media url is appear in the second field not in first. I am not sure where is the problem any suggestion would be great.

like image 845
Vignesh Pichamani Avatar asked Jul 16 '13 05:07

Vignesh Pichamani


People also ask

How do I add a duplicate plugin in WordPress?

Install and activate the plugin. In your WordPress dashboard, go to Posts > All when cloning posts, or Pages > All when cloning pages. Navigate to the page or post you want to copy, and click on Clone to duplicate it. Multiple pages or posts can be selected, and you can clone them all at once using Bulk Actions.

How do I add custom media uploader button to WordPress admin?

You may need to add the function wrapper in your plugin/theme file. This is the following: // UPLOAD ENGINE function load_wp_media_files() { wp_enqueue_media(); } add_action( 'admin_enqueue_scripts', 'load_wp_media_files' ); This will call the relevant JS files and CSS files if WP fails to load the upload manager.


2 Answers

UPDATED - scroll down

After too much of hard work and research and some customization I coded below compact few lines of code to use media uploader anywhere in wordpress. Just put code in some function and call that function wherever you want. The path of uploaded/selected file will be copied to text-box and then you can use it.

Hope this helps someone.!

// jQuery wp_enqueue_script('jquery'); // This will enqueue the Media Uploader script wp_enqueue_media(); ?>     <div>     <label for="image_url">Image</label>     <input type="text" name="image_url" id="image_url" class="regular-text">     <input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image">  </div> <script type="text/javascript"> jQuery(document).ready(function($){     $('#upload-btn').click(function(e) {         e.preventDefault();         var image = wp.media({              title: 'Upload Image',             // mutiple: true if you want to upload multiple files at once             multiple: false         }).open()         .on('select', function(e){             // This will return the selected image from the Media Uploader, the result is an object             var uploaded_image = image.state().get('selection').first();             // We convert uploaded_image to a JSON object to make accessing it easier             // Output to the console uploaded_image             console.log(uploaded_image);             var image_url = uploaded_image.toJSON().url;             // Let's assign the url value to the input field             $('#image_url').val(image_url);         });     }); }); </script> 

UPDATE: Just to add. You may need to add the function wrapper in your plugin/theme file. This is the following:

// UPLOAD ENGINE function load_wp_media_files() {     wp_enqueue_media(); } add_action( 'admin_enqueue_scripts', 'load_wp_media_files' ); 

This will call the relevant JS files and CSS files if WP fails to load the upload manager. This also removes console warnings.

like image 111
Rushabh Dave Avatar answered Oct 09 '22 03:10

Rushabh Dave


I'm using this method to use media uploader into my custom plugin.May be this would be help.

in the main theme file(index.php) add these.

wp_enqueue_style('thickbox'); // call to media files in wp wp_enqueue_script('thickbox'); wp_enqueue_script( 'media-upload');    // load script to admin function wpss_admin_js() {      $siteurl = get_option('siteurl');      $url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/js/admin_script.js';      echo "<script type='text/javascript' src='$url'></script>";  }  add_action('admin_head', 'wpss_admin_js'); 


In the admin_script.js file,

jQuery('#wpss_upload_image_button').click(function() {     formfield = jQuery('#wpss_upload_image').attr('name');     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('#wpss_upload_image').val(imgurl);  tb_remove();   jQuery('#wpss_upload_image_thumb').html("<img height='65' src='"+imgurl+"'/>"); } 

admin file(admin_settings.php),

<div id="wpss_upload_image_thumb" class="wpss-file">     <?php if(isset($record->security_image) && $record->security_image !='') { ?>        <img src="<?php echo $record->security_image;?>"  width="65"/><?php } else {    echo $defaultImage; } ?> </div> <input id="wpss_upload_image" type="text" size="36" name="wpss_upload_image" value="" class="wpss_text wpss-file" /> <input id="wpss_upload_image_button" type="button" value="Upload Image" class="wpss-filebtn" /> 

More details in my blog

like image 22
Sumith Harshan Avatar answered Oct 09 '22 04:10

Sumith Harshan