Justin Gable

Archive for the ‘Programming’ Category

Modifying Wordpress’ default method for inserting media

Working on a client site this week I wanted to change the way that Wordpress inserts media into posts. Right now if you were to upload a flash movie (FLV) and click the “Insert into post” button, Wordpress injects the title of the file linking to the file you just uploaded (ex - <a href="http://yoursite.com/wp-content/uploads/movie.flv">The Title</a>).

I wanted Wordpress to output the data in a custom shortcode (ex -[flash http://yoursite.com/wp-content/uploads/movie.flv]). I have a plugin that will take that shortcode and turn it into an embedded flash video automatically. This makes it incredibly easy for a client to add flash movies and mp3’s to their posts.

So what did I do to change the output?

Filters FTW

Searching around the source code of Wordpress I came across the filter media_send_to_editor in /wp-admin/includes/media.php on line 291 (there is also image_send_to_editor on line 63 for those who are interested). Now that I found the filter I just needed to write a basic plugin to modify the output.

<?php
/*
Plugin Name: EWM Modified Media Insert
Description: Modifies the default method for injecting media into posts
Author: Element Web Media
Version: 1.0
Author URI: http://www.elementwebmedia.com/
*/

class ewmInsertMedia {
  //class constructor
  function ewmInsertMedia () {
    add_filter('media_send_to_editor', array($this, modifyMediaInsert) , 10, 3);
  }
  //function that does the modifying
  function modifyMediaInsert($html, $send_id, $attachment) {
    $output = '[flash '.$attachment['url'].']';
    return $output;
  }
}
//instantiate the class
$ewmIM = new ewmInsertMedia();
?>

There are a total of 5 values that are available in the $attachment array:

  • url
  • menu_order
  • post_title*
  • post_excerpt*
  • post_content*

* It says “post_” but the values are not that of the actual post but the info of the file. I’m not sure why they chose those particular terms to describe the values, it just makes it confusing imho. I remember now that Wordpress stores media entry data in the wp_posts table. That’s why it’s post_title, post_excerpt, etc.

Going beyond

The shortcode API is awesome, if you haven’t messed around with it yet I urge you to take a look at the documentaion. This discovery has inspired me to start working on my own flash plugin, one that’s seamlessly implemented and without all the extra bloat (unnecessary header requests, etc). Maybe I’ll post it here when I’m done, we’ll see :).

A look at Wordpress 2.7

Sometime at the end of the year we can expect to see the next point version update for Wordpress (2.7). Those familiar with Wordpress know that with version 2.5 the blogging engine received a major, much needed, overhaul to the backend and a plethora of new features, so if you’re not expecting much out of 2.7 you’re in for a surprise. This update looks to add just as many features, if not more than 2.5, along with a restructuring of the backend layout, most notably the move from a horizontal-top navigation bar to an accordion style vertical-left nav.

This change and many of the other visual elements are being made to favor widescreen monitors. I’m glad they finally realized that the top style nav didn’t lend itself well to being extensible, with each plugin usually adding at least one nav link. Anyone who uses more than a few plugins knows what I’m talking about.

Add/Edit Post page (The Write-panel)

The write-panel received it’s fair share of “improvements”. Fans of the old school drag and drop elements will be happy to know that they’ve made a return. You can now drag and drop any of the element boxes between the right hand column and the column under the content box. They’ve gone a step further and added the ability to hide element boxes that you don’t want to see. Though, my thought is that these settings should be available through the settings menu rather than on the write-panel.

Plugin Repository

The change I’m most excited about is the ability to search the Wordpress plugin repository, select and install plugins, all from the backend of Wordpress! It’s all automated and very slick, no more having to download plugins, unzip them and then upload them manually. The only thing lacking about this addition is that the search flat out sucks. You’ll know this if you’ve ever tried to look for any plugins from http://wordpress.org/extend/plugins/, you get too many irrelevant plugins and typically the one you are looking for is buried at least a couple pages in.

Other notable additions

  • ability to make posts “sticky”
  • inline editing of posts from the front end
  • comments XMLRPC API
  • automated Wordpress updates
  • new HTTP request API (looking forward to that one)
  • built-in threaded comments (woo-hoo)
  • ability to reply to comments from the backend
  • added global settings for images (an addition size “Large”)
  • “Inbox” (this feature is not complete so I’m not sure exactly what it is going to be officially, however I’m guessing they are adding in some sort of built-in contact form… which would make a whole lot of sense)
  • QuickPress - a widget on the dashboard for quickly creating a post. (Honestly, kind of pointless, who doesn’t have time to click a link to get to the post page (there are 2 links on the page already)
  • media buttons consolidated to single “Add Media” button, this may only be for development and will probably go back to the 4 original buttons)

Not ready for primetime

Right now 2.7 isn’t ready for use on a production blog, some of the features are broken or completely disabled. However, if you want to get your hands dirty and try out the software you can get the latest nightly build here (scroll to the bottom and click the “Zip Archive” link). Have fun.