I want to prevent users from being able to set a Featured Image from the media library if its width is less than 100px. Originally I thought to use the ajax_query_attachments_args
filter, but it filters a WP_Query()
object which effectively cannot be used for this purpose because the meta_query
's meta_key
—which is _wp_attachment_metadata
—contains serialized data. This is what I'm currently trying:
function restrict_media_library_by_width($response, $attachment, $meta) {
if(isset($response['width']) && isset($response['height']) && $response['width'] >= 100) {
return $response;
}
return false;
}
add_filter('wp_prepare_attachment_for_js', 'restrict_media_library_by_width', 10, 3);
The result I see is that the Media Library modal pops up, loads an "empty" thumbnail, and the AJAX loader continues intermittently appearing and disappearing:
However, if I change the last conditional in my if statement to use ==
instead of >=
then it appears to work as expected for certain values:
function restrict_media_library_by_width($response, $attachment, $meta) {
if(isset($response['width']) && isset($response['height']) && $response['width'] == 100) {
return $response;
}
return false;
}
add_filter('wp_prepare_attachment_for_js', 'restrict_media_library_by_width', 10, 3);
It doesn't always work but I suspect I'm missing something here. Can someone please shed some light on this? Thanks!
floor()
toround()
inmedia-models.min.js
didn't fix the issue you describe. We also have to make sure we don't have plugins that are using this filter, like the ACF. In that case we have to increase the priority number. Otherwise thearray_filter( $posts )
part won't strip out the empty items. – birgire Mar 9 '15 at 11:34