3

Problem: Add a hook that automatically adds a short tag to video attachments when inserted from the Media Library (video) tab:

Update:

This works under the 'Media Library' tab — and doesn't break image inserts etc.:

add_filter('media_send_to_editor', 'my_filter_iste', 20, 3);

function my_filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $attachment = get_post($id); //fetching attachment by $id passed through

    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if (substr($mime_type, 0, 5) == 'video') { //checking mime-type
        $src = wp_get_attachment_url( $id );
        $html = '[video src="'.$src.'"]';  
    }
    return $html; // return new $html
}

Inserts something like this: [video src="path/to/video/sample.mp4"]

If you feel you can clean up the code, by any means, show us.

Thanks @wyrfel

3 Answers 3

6

There's a get_image_send_to_editor() function in wp-admin/includes/media.php that runs this: apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );. Try hooking that filter.

Edit: Help with the filter call... Your call to hook the callback would look like this:

add_filter('image_send_to_editor', array(&$MyClassReference, 'filter_iste'), 10, 8);

The 10 is the priority, you might need to adjust that...10 is the default. Your filter function needs to look pretty much like this:

function filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    ...
    return $html;
}

To derive what you need to do in the callback, i'd pretty much first dump all the parameters you're getting passed through to the screen. If you need something more, you can always fetch the whole attachment data and metadata using the $id you got passed through.

Basically, you could now try two different approaches:

  1. You check for the mime-type of the attachment and if it's a video, replace the whole $html content with your shortcode. You could add an option to the media settings that allows the user to configure if they want you to do so or not. You only need to hook the whole thing if they do. Edit: to check for video mime-types:

    $attachment = get_post($id);
    $mime_type = $attachment->post_mime_type;
    if (substr($mime_type, 0, 5) == 'video' && get_option('use_video_shorty_on_insert')) {
        ...
    }
    
  2. You run your $html content through a regexp replace to remove the bitsies the shortcode is wrapped in. What that regexp would look like depends on what the $html content typically looks like. I'd go for the first option, it's more solid and better UI. I understand that you might want to give the user the ability to change options for your shortcode, but this is another topic and i do believe it's possible to tweak the fields on the media popup to add some options there.

Edit 3: full example:

// this seems to be an additional filter running for images only
add_filter('image_send_to_editor', 'my_filter_iste', 20, 8);

function my_filter_iste($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $attachment = get_post($id); //fetching attachment by $id passed through
    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if (substr($mime_type, 0, 5) == 'video') { //checking mime-type
        //if a video one, replace $html by shortcode (assuming $url contains the attachment's file url)
        $html = '[video src="'.$url.'"]';
    }
    return $html; // return new $html
}

Ok...wrong filter, apparently...try this:

// this seems to run when inserting a video via the 'From URL' tab
add_filter('video_send_to_editor_url', 'my_filter_vsteu', 20, 3);

function my_filter_vsteu($html, $href, $title) {
    $html = '[video src="'.$href.'"]';
    return $html;
}

Ahh well, we've got one more:

// this seems to run generically for any media item from the 'Upload' tab
add_filter('media_send_to_editor', 'my_filter_mste', 20, 3);

function my_filter_mste($html, $send_id, $attachment) {
    if (substr($attachment->post_mime_type, 0, 5) == 'video') {
        $href = wp_get_attachment_url($attachment->ID);
        $html = '[video src="'.$href.'"]';
    }
    return $html;
}
12
  • @PHearst see edits
    – wyrfel
    Feb 22, 2011 at 10:34
  • @PHearst: see new edit...you can't just reference $post without fetching it first...you were close, though, but the filter only get's the $id, which is the 'post_id' of your attachment, so once you got the complete post (i called it $attachment), you can then reference the post_mime_type property.
    – wyrfel
    Feb 22, 2011 at 16:29
  • @PHearst: Also added a check for a fictive 'use_video_shorty_on_insert' option as suggested before (which should be boolean).
    – wyrfel
    Feb 22, 2011 at 16:36
  • @PHearst: Just stack my snippets together replacing the ... with the next snippet...and in the last one with your $html = '[video src="'.$url.'"]'; line. That should do it. If you don't implement the option, remove that part from my if... clause.
    – wyrfel
    Feb 23, 2011 at 8:18
  • @PHearst: Looks like i confused you. Change 6th line to if (substr($mime_type, 0, 5) == 'video') {.
    – wyrfel
    Feb 23, 2011 at 15:45
2

Answer to my question-

just change:

if ($uploadType == 'video') {

to:

if (get_post_mime_type( $id ) == "application/pdf") {
0

About the Video mime check, this works:

function my_filter_mste($html, $send_id, $attachment_id) {

global $wpdb;

$postDetails = $wpdb->get_row( $wpdb->prepare("SELECT post_mime_type FROM $wpdb->posts WHERE ID = %s", $send_id) );
$type = explode("/", $postDetails->post_mime_type);
$uploadType = strtolower($type[0]);

if ($uploadType == 'video') {
$href = $attachment_id['url']; 
$html = '[stream flv='.$href.' mp4='.$href.' embed=false share=false width=480 height=360 autostart=false /]';
}
return $html;

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.