Monthly Archive for July, 2007

Modifying Allowed Upload Types in Wordpress

Several times recently in #wordpress, I’ve seen people asking how they could modify the list of allowed file types used in the file uploader on the Write Post page.

Since this information isn’t readily available (apparently) and to a lesser degree because I’m tired of not being able to find my previous code (causing me to re-type it all each time), I thought I’d throw together another quick WordPress hack guide.

Introduction

When you attempt to upload a file in WordPress (2.0+ I believe) that is not in the default list of acceptable file types1, you will receive the following error:

WordPress File Upload Error

As of WordPress 2.2, there are 35 allowed file types configured in the default install. While there’s no admin-based tool for editing this list (nor any plugins that I’m aware of), it’s not at all difficult to add your own…

The Code

Upload filetypes are checked by the function wp_check_filetype in wp-includes/functions.php (around line 1,000 in my current copy of trunk). Looking at the code, we see that the default array is passed into the upload_mimes filter, allowing you to easily add and remove types at will using a quick plugin hook.

So how do we do it? Well, first you need to add a new plugin hook. In your theme’s functions.php file, add this line:

[php]
add_filter(’upload_mimes’, ‘custom_upload_mimes’);
[/php]

You can, of course, replace custom_upload_mimes with your own preferred function name. Just make sure it’s something unique that ideally won’t cause any naming conflicts later on2.

Now we’ve got a hook that tells WordPress to take the array of file types passed into the upload_mimes hook and hand it to the function custom_upload_mimes. Great, but where’s our function?

No problem, I’ve got it all ready for you. Open back up your theme’s functions.php file and toss in this code:

[php]
function custom_upload_mimes ( $existing_mimes=array() ) {

// add your ext => mime to the array
$existing_mimes['extension'] = ‘mime/type’;

// add as many as you like

// and return the new full result
return $existing_mimes;

}
[/php]

Note that the function accepts the $existing_mimes array, adds a new file type (with the extension “extension” and of the mime type “mime/type”), and then returns the whole array.

Replace extension with your extension (no period before it, just the textual extension) and then Google to find out its mime type3.

Add as many new types as you like, simply by copying the example line and filling in your values. Also, make sure you name the function the same thing you used in the hook, assuming you don’t like my convention. Save your new functions.php file and you’re good to go!

Removing Existing Types

What if you want to remove an existing allowed type, instead of adding your own new type? Well, that’s even easier!

Replace the line $existing_mimes['extension'] = ‘mime/type’; with unset( $existing_mimes['extension'] ) and you’re done. For example, to prevent users from uploading .exe files, you would use:

[php]
unset( $existing_mimes['exe'] );
[/php]

Good luck, hope this helps!

  1. For example, Microsoft Installer files are not allowed by default, so track down an .msi file and try to upload it for a quick test. [back]
  2. I like to use the prefix ‘custom_’ simply because it’s easy to tell later on that this is a custom modification. [back]
  3. Googling for “.zip mime type”, for example, should give you any number of sites listing a whole slew of mime types. Yours should be in the same <category>/<type> format as the others. Zip is “application/zip”. [back]

Why the NewsGator API Still Sucks

The Idea
I had a brilliant idea yesterday, and couldn’t wait to get home so I could start coding it. Things were moving along great. I had my database created, I’d gotten Code Igniter configured and running with my standard set of libraries, helpers, etc. and I was hammering away at some code.

Damnation
And then it happened. I got to the heart and soul of the application: the part that interacted with the NewsGator API. Almost instantly, my entire world crashed in around me.

What’s that? You’ve never dealt with the NewsGator API? Consider yourself lucky. Only murderers and rapists should be doomed to such a fate. The NewsGator API is by far, and without a doubt, the worst API I have ever dealt with - and I’ve dealt with quite a few in the past.

Continue reading ‘Why the NewsGator API Still Sucks’

Using jQuery in Wordpress

It’s been a while since I actually encountered this particular nugget of advice1, but I thought I’d go ahead and make a quick post out of it anyway.

This is the kind of totally random and arbitrary development structure Wordpress has adopted that doesn’t really seem to ever be documented anywhere. If you don’t already know this kind of thing, you could very well be in store for major development and debugging headaches.

Anyway, here we go… If you’re using the built-in jQuery Javascript library that’s included in Wordpress since version 2.2, don’t use the handy-dandy $() function. In Wordpress, $() is reserved for the Prototype library, which is also bundled2.

Instead, for interoperability, be sure to use jQuery() instead, which should accomplish the same thing.

A bad example:

[javascript]
var username = $(’#username’).val();
[/javascript]

If Prototype were to be loaded on the page this snippet of JS is running on, it would throw an error, since it uses a different pattern for selecting DOM elements.

A good example:

[javascript]
var username = jQuery(’#username’).val();
[/javascript]

This line should work on any page, regardless of library conflicts. It’s a couple extra characters to type, but in the end it’s really for the best - you get portability, and it’s more self-explanatory which library is being used when you go back to look at this code in 6 months.

I’m planning another, similar, Javascript-related post as soon as I get a few more minutes to make it coherent. Stay tuned, and happy coding!

  1. Pointed out by rob1n in #wordpress, BTW. [back]
  2. In all fairness, I suppose this makes sense. Prototype was added first. [back]

The Most Amazing BBQ

So I was on my own for dinner tonight. My mom’s finishing up painting one of the bedrooms in our house, and I’ve exhausted all my broke friends’ dinners out for a while1, so it was up to me to come up with something to eat.

I’m also more than sick of fast food and pizza - there are only so many times a guy can eat Chick-Fil-A, Wendy’s, Burger King, and Dominos in a given month - so it was time for something new.

About 6 months ago, there was this little hole in the wall place that I saw advertising BBQ on my way home. It’s stuck right at the corner of two streets, in a curve. One room and a little 2-person porch, with a big window in the front. A couple of nights I saw it open, and then it seemed to disappear for several months. Finally, last week I saw it pop back open.

The last time I’d seen it open, then noticed when it disappeared, I’d mentioned it to my mom, who is a bit of a skeptic when it comes to little back-woods places to eat. She’d said she wouldn’t dare try it unless someone she knew recommended it, even though she couldn’t give a legitimate reason why not2.

So before it started to rain tonight, I ran back out and stopped by. It’s run by this old guy (probably in his 60’s) and apparently his wife. The guy cooks the food during the morning and afternoon, and then runs the counter, while his wife puts everything together as he shouts at her in the kitchen.

When I showed up, there was another older guy in the front room (erm, half room) waiting on his food, talking with the guy who runs the place. I asked what was good, and they both recommended the chicken and ribs plate. I’m a huge fan of BBQ chicken, so I’d been planning on getting it anyway - the recommendations just sealed the deal. I also ordered one of their pulled pork BBQ plates, figuring that you can’t go wrong with that pseudo-BBQ3.

While I was waiting, I was talking with the old guy who runs the place. Apparently he’s smoked like a chimney for about 500 years now, and he’s on dialysis, walks with a cane, and can barely see. But my god does he know how to make BBQ. The chicken, the ribs, and even the pork were the best I’ve ever eaten. If the lightning storm outside had struck me down mid-bite, I could have died happy and content with the delightful stinging sensation left on my lips for all eternity.

Never ever dismiss the hole in the wall restaurant, especially the one with a BBQ sign out front!

  1. Apparently I’m the only one who likes to be able to go out drinking at least once a week. [back]
  2. You can see the DHEC rating in the window from the road, and it passed with flying colors — an ‘A’. [back]
  3. Which is really what people down here in the southeast think is “real” BBQ. Fortunately, I’ve had dealings with Texans and their differing definition… [back]