Skip to content

Instantly share code, notes, and snippets.

@sethrubenstein
Created August 12, 2015 16:38
Show Gist options
  • Save sethrubenstein/d45581883c93c4e3c1ae to your computer and use it in GitHub Desktop.
Save sethrubenstein/d45581883c93c4e3c1ae to your computer and use it in GitHub Desktop.
WP Media Library Hide Sizes
<?php
/**
* This hook tells the media uploader to only display select sizes to users.
* @param $sizes the original image sizes available to the image editor.
* @return $new_sizes the original $sizes and the new sizes we're adding.
* @author Mahder
*/
function sr_add_image_sizes_to_editor($sizes) {
$new_sizes = array();
// Add these sizes to the size list...
$added_sizes = array('full-width', 'full-width-wide-display');
foreach( $added_sizes as $size ) {
$new_sizes[$size] = $size;
}
// This preserves the labels in $sizes, and merges the two arrays
$new_sizes = array_merge( $new_sizes, $sizes );
// Remove the thumbnail size
unset( $new_sizes['thumbnail'] );
// Check for a specific taxonomy like a display type and remove some sizes if that tax is not selected.
global $post;
$wide_display_check = wp_get_post_terms( $post->ID, 'display_type', array("fields" => "all") );
$post_format = $wide_display_check[0]->slug;
if ( $post_format != 'wide-display' ) {
unset( $new_sizes['full-width-wide-display'] );
}
return $new_sizes;
}
add_filter('image_size_names_choose', 'sr_add_image_sizes_to_editor', 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment