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 = ''.$src.'';
}
return $html; // return new $html
}
Inserts something like this:
If you feel you can clean up the code, by any means, show us.
Thanks @wyrfel
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:
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:
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:
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:
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:
Ok…wrong filter, apparently…try this:
Ahh well, we’ve got one more:
Answer to my question-
just change:
to:
About the Video mime check, this works:
Hope it helps!