Justin Gable

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.

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 :).

Comments are closed.