Skip to content

Instantly share code, notes, and snippets.

@WerdsWords
Last active December 20, 2015 15:19
Show Gist options
  • Save WerdsWords/6153808 to your computer and use it in GitHub Desktop.
Save WerdsWords/6153808 to your computer and use it in GitHub Desktop.
#33: media_send_to_editor
<?php
/**
* Disallow inserting 'full' sizes images into the editor.
*
* @see wp_ajax_send_attachment_to_editor(), media_upload_form_handler()
*
* @param string $html The unslashed HTML to send to the editor.
* @param in $id The attachment id.
* @param array $attachment An array of attachment attributes.
*
* @return string The filtered HTML sent to the editor.
*/
function wpdocs_no_full_images( $html, $id, $attachment ) {
$post = get_post( $id );
// If it's an image and size is 'full', pass 'large' instead
if ( 'image' == substr( $post->post_mime_type, 0, 5 ) ) {
if ( ! empty( $attachment['image-size'] ) && 'full' == $attachment['image-size'] ) {
$url = $attachment['url'];
$align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none';
$size = 'large';
$alt = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
$rel = ( $url == get_attachment_link( $attachment_id ) );
return get_image_send_to_editor( $id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt );
}
}
return $html;
}
add_filter( 'media_send_to_editor', 'wpdocs_no_full_images', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment