diff --git a/wp-admin/js/media-upload.js b/wp-admin/js/media-upload.js index caf609de44..4713d068a1 100644 --- a/wp-admin/js/media-upload.js +++ b/wp-admin/js/media-upload.js @@ -108,9 +108,12 @@ var tb_position; } ) ); workflow.on( 'update:insert', function( selection ) { - this.insert( '\n' + selection.map( function( attachment ) { - return wp.media.string.image( attachment ); - }).join('\n\n') + '\n' ); + this.insert( selection.map( function( attachment ) { + if ( 'image' === attachment.get('type') ) + return '\n' + wp.media.string.image( attachment ) + '\n'; + else + return wp.media.string.link( attachment ) + ' '; + }).join('') ); }, this ); workflow.on( 'update:gallery', function( selection ) { diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index 0c88864d2e..88425e0a8c 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -371,6 +371,24 @@ window.wp = window.wp || {}; var mceview = wp.mce.view; wp.media.string = {}; + + wp.media.string.link = function( attachment ) { + var linkTo = getUserSetting( 'urlbutton', 'post' ), + options = { + tag: 'a', + content: attachment.get('title') || attachment.get('filename'), + attrs: { + rel: 'attachment wp-att-' + attachment.id + } + }; + + // Attachments can be linked to attachment post pages or to the direct + // URL. `none` is not a valid option. + options.attrs.href = ( linkTo === 'file' ) ? attachment.get('url') : attachment.get('link'); + + return wp.html.string( options ); + }; + wp.media.string.image = function( attachment, props ) { var classes, img, options, size; diff --git a/wp-includes/media.php b/wp-includes/media.php index 6d72e3d30b..87762f7de2 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1246,6 +1246,7 @@ function wp_prepare_attachment_for_js( $attachment ) { 'title' => $attachment->post_title, 'filename' => basename( $attachment->guid ), 'url' => $attachment_url, + 'link' => get_attachment_link( $attachment->ID ), 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'author' => $attachment->post_author, 'description' => $attachment->post_content,