Skip to content

Instantly share code, notes, and snippets.

@ascottmccauley
Created June 18, 2015 15:43
Show Gist options
  • Save ascottmccauley/550b8ba1e9861be15064 to your computer and use it in GitHub Desktop.
Save ascottmccauley/550b8ba1e9861be15064 to your computer and use it in GitHub Desktop.
Better WordPress Media Handling
if ( current_user_can( 'edit_posts' ) ) {
// Tell the TinyMCE editor to use a custom stylesheet
// See http://codex.wordpress.org/Function_Reference/add_editor_style
add_theme_support( 'editor_style' );
add_editor_style( 'assets/css/editor-style.css' );
// Prevent WordPress from uploading duplicate files
// Checks to see if both filenames match and then overwrites only if the filesizes are different
function groundup_remove_duplicate_attachment( $file ) {
$upload_dir = wp_upload_dir();
// replace spaces with dashes just like WordPress does
$filename = str_replace( ' ', '-', $file['name'] );
if ( file_exists( $upload_dir['path'] . '/' . $filename ) ) {
// Compare filesizes
if ( filesize( $upload_dir['path'] . '/' . $filename ) != $file['size'] ) {
// query the attachment so that it will be deleted and removed from media library
$args = array(
'numberposts' => 1,
'post_type' => 'attachment',
'meta_query' => array(
array(
'key' => '_wp_attached_file',
'value' => trim($upload_dir['subdir'] . '/' . $filename, '/')
)
)
);
$attachment_file = get_posts($args);
wp_delete_attachment( $attachment_file[0]->ID, true );
} else {
// remove filename and replace with an error so the file will not be re-uploaded
$file = array('name'=>$filename, 'error'=>'Image ' . $filename . ' already exists.');
}
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'groundup_remove_duplicate_attachment' );
// change image suffixes to <filename>-<image_size>
// TODO: Don't think this will work if multiple sizes share width or height
// Look into finding out which image size it is another way!
function groundup_image_suffix( $image ) {
// Split the $image path into directory/extension/name
$info = pathinfo( $image );
$dir = $info['dirname'] . '/';
$ext = '.' . $info['extension'];
$file_name = wp_basename( $image, "$ext" );
$image_name = substr( $file_name, 0, strrpos( $file_name, '-' ) );
// Get image information
$img = wp_get_image_editor( $image );
// Get image size, width and height
$img_size = $img->get_size();
// Get new image suffix by comparing image sizes
$image_sizes = get_intermediate_image_sizes();
foreach ( $image_sizes as $size ) {
$rename = false;
$sizeInfo = get_image_size_data( $size );
if ( $img_size['width'] == $sizeInfo['width'] && $img_size['height'] <= $sizeInfo['height'] ) {
$rename = true;
} elseif ( $img_size['height'] == $sizeInfo['height'] && $img_size['width'] <= $sizeInfo['width'] ) {
$rename = true;
}
if ( $rename == true ) {
// Rename image
$new_name = $dir . $image_name . '-' . $size . $ext;
// Rename the intermediate size
$rename_success = rename( $image, $new_name );
if ( $rename_success ) {
return $new_name;
}
}
}
// do nothing if not renamed
return $image;
}
add_filter( 'image_make_intermediate_size', 'groundup_image_suffix' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment