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.

7 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 … [...]

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.



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