Loading...


Loading...

Welcome!

Sign up for a free account, or log in here.

Member Login

You can login by following the link.

Not a Member? Sign up! It's Free!

Please feel free to register.

Click to log in.

18 comments

Multiple Media Uploads in WordPress’ Functions.php

Recently I had a real problem with a theme that I’ve never really run into before. I needed a way to upload an image in the admin backend. I had never seen a way to do this anywhere. I looked through the admin file, but couldn’t find anything.

Then I found a great post by Matt at Webmaster-Source. He detailed a way to upload an image in the functions.php file, absolutely perfect for what I wanted. But my problem was that I needed multiple media uploads on the page, which he nearly had but just needed a slight alteration.

Basically add a simple form with these two lines in:

<input id=”upload_image” type=”text” size=”36″ name=”upload_image” value=”" />
<input id=”upload_image_button” type=”button” value=”Upload Image” />

So here is his javascript (jquery) code that handles it:

$(document).ready(function() {
 $(‘#upload_image_button’).click(function() {
  formfield = $(‘#upload_image’).attr(‘name’);
  tb_show(”, ‘media-upload.php&type=image&TB_iframe=true’);
  return false;
 });

 window.send_to_editor = function(html) {
  imgurl = $(‘img’,html).attr(‘src’);
  $(‘#upload_image’).val(imgurl);
  tb_remove();
 }
});

So I altered it just a tiny little bit and here is the alteration. The first bit is to take his first function and slightly alter it for each instance of the form, the ‘upload_image_button‘ id and the formfield variable that selects the ‘upload_image‘ id:

 $(‘#upload_image_button’).click(function() {
  formfield = $(‘#upload_image’).attr(‘name’);
  tb_show(”, ‘media-upload.php?type=image&TB_iframe=true’);
  return false;
 });

In the second function, use the variable that he had set up earlier (formfield):

 window.send_to_editor = function(html) {
  imgurl = $(‘img’,html).attr(‘src’);
  $(‘#’ + formfield).val(imgurl);
  tb_remove();
 }

That should do it. Every time the media file is selected, then the submit/upload button event puts the media (image, in my case) url into the input box. Use your functions.php know-how to save it as an option for your theme, and use it as a background etc.,

Trackbacks

[...] Read this article: Multiple Media Uploads in WordPress' Functions.php :: The English … [...]

[...] Rich from The English Guy Web Design. He adapted the same code for use with multiple entries on a single page. That was exactly what I [...]

[...] que me han servido de fuente, como estos: – Using the WordPress Uploader in Your Plugin or Theme – Multiple Media Uploads in WordPress’ Functions.php – Y sobre todo la ayuda del gran libro Professional WordPress Plugin DevelopmentVamos a crear dos [...]

Jude says:

It seems that the send_to_editor function has never been called. any idea what am i missing? i used wordpress 3.0 beta 2.

rich says:

I haven’t got WP3 installed yet, but I’ll do so tonight and give it a try and report back.

Justin says:

Thanks for sharing this, however, I feel like I must be missing something. It looks to me like the first set of code, that controls the click function, is exactly the same in your ‘altered’ code? Where is the change? How does this enable having multiple instances of the form on a single page?

I’m not a JavaScript expert, so I might just be missing something simple.

Thanks!

MatthewRuddy says:

I tried this on wp 2.9.2 and it just doesn’t seem to work for me no matter what I try. I can only have one working “upload image” button, no more (as in, only the first button out of 2 or more will work). Really need a hand. Could you post your entire script? Think I might be getting a bit confused between yours and the original. Also in the original, “jQuery” is used instead of “$”, why is this?

Brad says:

What the jquery vs $. So that there are no collisions with other javascript frameworks that use the $, like mootools, extjs, etc…

Basic information on this is here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

-Brad

kathy says:

this is exactly what i am working on now, but it looks like Justin is right… the 2 bits of code (before and after) look identical. What did you change? I was thinking an .each() function should work, but so far i haven’t sorted it out yet.

alex says:

Curious… has anyone figured this out?

Hank says:

has anyone figured this out yet – looks like there is still identical code in the two functions…

ebe says:

well i am not a jquery expert and had the need to upload many images in same file, i found a workaround not the best but it works, hope a jquery guru can do it better. Call the onclick event in button and pass the input id and walla! cheers.

function uploaderWeb(container){

formfield = jQuery(‘#’+container).attr(‘name’);
tb_show(”, ‘media-upload.php?type=image&TB_iframe=true’);
//return false;

window.send_to_editor = function(html){
imgurl = jQuery(‘img’, html).attr(‘src’);
jQuery(‘#’+container).val(imgurl);
tb_remove();
}

}

Andrea says:

Multiple forms (2 in this case) using class selector, no need of coding multiple listeners, HTML could be dinamically generated while the script remains the same.

AND function proxying..so we keep original functionality of media-upload.php in case we just want to upload an image into the content of the post.

The HTML:

Upload Image 1

Enter an URL or upload 1st image

Upload Image 2

Enter an URL or upload 2nd image

The JS:

jQuery(document).ready(function() {

var formlabel = 0;
var old_send_to_editor = window.send_to_editor;
var old_tb_remove = window.tb_remove;

jQuery(‘.upload_image_button’).click(function(){
formlabel = jQuery(this).parent();
tb_show(”, ‘media-upload.php?type=image&TB_iframe=true’);
return false;
});

window.tb_remove = function() {
formlabel = 0;
old_tb_remove();
}

window.send_to_editor = function(html) {
if(formlabel){
imgurl = jQuery(‘img’,html).attr(‘src’);
formlabel.find(‘input[type=text]‘).value(imgurl);
tb_remove();
} else {
old_send_to_editor
}
}

});

Works ok!
Enjoy!

Andrea says:

The HTML part…

<table>
<tr valign="top">
<th scope="row">Upload Image 1</th>
<td><label for="image1">
<input id=image1" type="text" size="36" name="image1" value="" />
<input class="upload_image_button" type="button" value="Browse.." />
<br />Enter an URL or upload image1
</label></td>
</tr>
<tr valign="top">
<th scope="row">Upload Image 2</th>
</td>
<td><label for="image2">
<input id="image2" type="text" size="36" name="image2" value="" />
<input class="upload_image_button" type="button" value="Sfoglia…" />
<br />Enter an URL or upload image2
</label></td>
</tr>
</table>


jQuery(document).ready(function() {

// I changed the trigger to a class.
jQuery('.image_uploader_button').click(function() {
// I switched the id for the this-prev selector that sends the name
of the input field that proceeds it.
formfield = jQuery(this).prev().attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});

window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
// I then called the name and joined with # for the id.
jQuery("#"+formfield).val(imgurl);
tb_remove();
}

});

Now I can run as many image uploads on the page as I want.

Um, “altered code” is exactly the same… On the other had, I did pretty much exactly what @Ethan Hackett did. That works great.

SP007 says:

Can any one provide me the full source code



Leave a Reply



You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


The English Guy on Twitter

Raised Paper WordPress Theme

Raised Paper WordPress Theme screenshot
Theme Price

Onyx WordPress Theme

Onyx WordPress Theme screenshot
Theme Price

Victoriana WordPress Theme

Victoriana WordPress Theme screenshot
Theme Price

Expression Blue WordPress Theme

Expression Blue WordPress Theme screenshot
Theme Price

Premium WordPress Theme Release: Natural Green

Premium WordPress Theme Release: Natural Green screenshot
Theme Price

Theme Release: Ikon WordPress Theme

Theme Release: Ikon WordPress Theme screenshot
Theme Price

Shocking WordPress Theme Release

Shocking WordPress Theme Release screenshot
Theme Price

Digitalis WordPress Theme Release

Digitalis WordPress Theme Release screenshot
Theme Price

WordPress Theme: OpenAir Theme

WordPress Theme: OpenAir Theme screenshot
Theme Price

WordPress Theme: Very English

WordPress Theme: Very English screenshot
Theme Price

News Print v3.0 WordPress Theme Released

News Print v3.0 WordPress Theme Released screenshot
Theme Price

Reclamation WordPress Theme

Reclamation WordPress Theme screenshot
Theme Price