Add class=”media_type” when media is inserted into editor

So thanks to @amritanshu and @wycks github’s code, here is the solution for those in need to add a class with the media type to the attachment url before it gets inserted in the editor :

if ( ! function_exists( 'epc_add_class_pdf' ) ) :

    function epc_add_class_pdf( $html, $id ) {

        $attachment = get_post( $id );
        $mime_type = $attachment->post_mime_type;

        // I only needed PDF but you can use whatever mime_type you need
        if ( $mime_type == 'application/pdf' ) {
            $src = wp_get_attachment_url( $id );
            $html="<a class="pdf" href="". $src .'">'. $attachment->post_title .'</a>';
        }

        return $html;
}
endif;
add_filter('media_send_to_editor', 'epc_add_class_pdf', 20, 3);

Leave a Comment