I am creating a baseball website with multiple Authors. From past experience, no matter how well you know someone personally, it doesn’t mean they will follow or even read your instructions.
That being said, I would like to require that any image an Author decides to “Use as featured image” be at minimum of 640px wide and a minimum of 360px tall.
I have required that each post have a Featured Image using the WyPiekacz plugin; the post will not publish without a Featured Image. I have blocked the ability for an Author to hotlink to another site by removing the “From URL” tab in Add Media using Bainternet’s code.
Now I need to require that any image used as the featured image is at least 640px by 360px. I am no means a coder, but I have been playing around with and trying to use Maor Barazany’s code as a starting point, but to no avail. His code forces minimum dimensions for any image that is uploaded.
well if you are using WyPiekacz plugin; as you said for checking that featured image is uploaded, you can tweak it little bit to check that if there is featured image it is of minimum dimesions as you required.
You can change above code in wypiekacz.php to,
well i don’t understand what you mean by “Media Library Tab”.
I checked the core and apparently there is little room for maneuvering.
/wp-admin/includes/media.php is where Add Media tabs are generated
The function get_media_item in line 1034 is the one that renders the attachments/media table. I can’t see any filter available in it or the previous functions that call this one.
Some references and code samples around the issue.
I guess an alternative solution would be to change the title of the uploaded images and append its dimensions. I’m not sure about changing the post_title of an uploaded file, but renaming the file itself can be achieved with this two filters: sanitize_file_name and wp_handle_upload_prefilter
Not a complete answer and not for the bounty, just a proof that the basic concept works:
Just a 60secondsSnippet and has one big problem: This will fire for every upload, and not only if someone is up to add featured image, because we don’t have a way to get the context of the image uploader. There are some ways to work around it, basically with some js manipulation.
I should be working right now, and don’t have the time to experiment with it. But i wanted to help as much as i could, and maybe this is a starting point for others.
cheers
This isn’t the most elegant answer… but it works! The ‘WyPiekacz’ plugin, although cool, has not been updated in three years.
The most elegant solution with the best user experience would use JavaScript to handle this for both Quick Edit and the post edit page. Then for good luck I’d add something to the
update_post_metadata
filter (which totally works for preventing the featured image but won’t give a warning since it’s run with AJAX).Admin notices won’t show up because WordPress redirects, and even then wouldn’t show up on a Quick Edit (my method does show a warning on Quick Edit although it is not styled).
Here is one way of making sure there is a properly sized thumbnail before displaying it.
First, create this helper function:
Then you can now check before displaying the post thumbnail:
This will do whatcha need 🙂
It’s using get_the_post_thumbnail which may also help you so you didn’t need to create a bunch of fn code that WordPress can already handle for you, just a thought.
This uses
$thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'rand','meta_key' => '_thumbnail_id'));
to grab a random one if one is not present, this may help you moving forward.This bit
echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
notice the'small-thumb'
it gets matched to those add_image_size fn’s we put together up a few lines. So if you hadadd_image_size( 'small-square', 100, 100, true );
you could call on'small-square'
alternatively.Cheers