I’m using a function to create additional fields on media upload. They are important only for images and videos, but useless and even confusing to my users if they are uploading audio.

This is the function:

function give_linked_images_data($html, $id, $caption, $title, $align, $url, $size, $alt="" ){
    $classes="img image-link";
    if (get_post_meta($id, "pop_title") == '') $poptitle=""; else $poptitle=" data-title="". get_post_meta($id, "pop_title", true) .'"';    
    $html = preg_replace('/(<a.*?)>/', '$1 data-toggle="lightbox" '. $poptitle .' ' . $classes . '" >', $html);
    return $html;
}

The functions is added with

add_filter('image_send_to_editor','give_linked_images_data',10,8);

…and I have a similar function using

add_filter('media_send_to_editor','give_linked_images_data',10,8);

…but it runs on video and audio upload. How can I detect if the media being uploaded is audio, to disable the custom fields?

2 Answers
2

We can simplify the mime type checks, with the following boolean functions:

  • wp_attachment_is( 'image', $id )

  • wp_attachment_is( 'video', $id )

  • wp_attachment_is( 'audio', $id )

where $id is the attachment ID.

The attachment’s ID is actually one of the input arguments for the media_send_to_editor filter callback.

We also have:

  • wp_attachment_is_image( $id )

that’s a wrapper for wp_attachment_is( 'image', $id ).

References:

  • wp_attachment_is()
  • wp_attachment_is_image()
  • media_send_to_editor

Leave a Reply

Your email address will not be published. Required fields are marked *