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.

  • Can't answer your question, but i have to say thank you for a nice collection of handy filters and functions ;) Commented Mar 27, 2012 at 2:08
  • ditto, Ole Henrik :)
    – brasofilo
    Commented Mar 30, 2012 at 18:35
  • What about images that are attached to posts, but are not intended to be used as the Post Featured Image? Is it absolutely impossible that every post will only ever have Post Featured Images attached? Commented Apr 12, 2012 at 21:10
  • No, but every post is required to have a featured image. This is the main reason Maor Barazany's code (link above)to force min. dimensions on upload would not work (or ungestaltbar's code below). Those code snippets force ALL uploaded images to the minimum dimensions. Users may add additional images to their posts, but the "Featured Image" must be large enough to fit the theme without stretching. This is also why I want an "Image Dimensions" column on the "Media Library" tab of the uploader. Users will quickly be able to choose an image large enough for a featured image. Commented Apr 12, 2012 at 21:19
  • I get that you want all posts to have a Featured Image. But will that always be the only image attached to posts? Or could posts have other images in addition to the Featured Image? Commented Apr 12, 2012 at 21:21

6 Answers 6


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.

$has_thumbnail = false;
            if ( ( $post_id > 0 ) && function_exists( 'has_post_thumbnail' ) ) {
                $has_thumbnail = has_post_thumbnail( $post_id );

            $has_thumbnail = apply_filters( 'wypiekacz_check_thumbnail', $has_thumbnail, $post_id, $post_data );

            if ( !$has_thumbnail ) {
                $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) is required.', 'wypiekacz') );

You can change above code in wypiekacz.php to,

$has_thumbnail_proper_dimension = false;
        if ( ( $post_id > 0 ) && function_exists( 'has_post_thumbnail' ) ) {
            $has_thumbnail = has_post_thumbnail( $post_id );
              list($url, $width, $height) = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), "Full");
                echo $imgsrc[0];
              if($width>=640 and $height>=360){

        $has_thumbnail = apply_filters( 'wypiekacz_check_thumbnail', $has_thumbnail, $post_id, $post_data );

        if ( !$has_thumbnail ) {
            $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) is required.', 'wypiekacz') );
        if ( !$has_thumbnail_proper_dimension ) {
            $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) should be atleast 640x360.', 'wypiekacz') );

well i don't understand what you mean by "Media Library Tab".

  • You are a beautiful man! Works perfectly! The "Media Library Tab" is in the media upload screen, like when you click on "Set Featured Image" or "Upload/Insert" for media. Here is a screenshot of my idea - picasaweb.google.com/lh/photo/… Commented Apr 12, 2012 at 15:48
  • I did make one change. list($url, $width, $height) = wp_get_attachment_image_src($post_id, 'full'); changed to list($url, $width, $height) = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), "Full"); echo $imgsrc[0]; to check the dimensions of only the Featured Image, and not all attached images. Commented Apr 12, 2012 at 23:15
  • @TravisPflanz This is the function that outputs the row of "Medial Library Tab" codex.wordpress.org/Function_Reference/get_media_item There is no filter here so i dont think you can change it wihtout hooking into wordpress file.... You can accept answer/upvote if usefull...... Commented Apr 13, 2012 at 7:21

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

  • So, what you're saying is that it could be possible... Maybe? I really am no coder, so I am at the mercy of others when it comes to writing functions. Any support would be great. Commented Apr 5, 2012 at 20:48

This isn't the most elegant answer... but it works! The 'WyPiekacz' plugin, although cool, has not been updated in three years.

add_action('transition_post_status', 'check_featured_image_size_after_save', 10, 3);

function check_featured_image_size_after_save($new_status, $old_status, $post){
  $run_on_statuses = array('publish', 'pending', 'future');
  if(!in_array($new_status, $run_on_statuses))

  $post_id = $post->ID;
  if ( wp_is_post_revision( $post_id ) )
    return; //not sure about this.. but apparently save is called twice when this happens

  $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), "Full" );
    return; //separate message if no image at all. (I use a plugin for this)

  $image_width = $image_data[1];
  $image_height = $image_data[2];

  // replace with your requirements.
  $min_width = 900;
  $min_height = 400;
  if($image_width < $min_width || $image_height < $min_height){
    // Being safe, honestly $old_status shouldn't be in $run_on_statuses... it wouldn't save the first time!
    $reverted_status = in_array($old_status, $run_on_statuses) ? 'draft' : $old_status;
      'ID' => $post_id,
      'post_status' => $reverted_status,
    $back_link = admin_url("post.php?post=$post_id&action=edit");
    wp_die("Featured Image not large enough, must be at least ${min_width}x$min_height. Reverting status to '$reverted_status'.<br><br><a href='$back_link'>Go Back</a>");

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).

  • Has this been tested? I haven't needed to do this in a while, but it's nice to bookmark. Commented Feb 20, 2015 at 18:59
  • Maybe on Monday I can run it on our staging server, but I do have something 99% similar to this running on production. Commented Feb 22, 2015 at 16:43

Not a complete answer and not for the bounty, just a proof that the basic concept works:

function wpse_attachment_dimension_check( $form_fields, $post ) {

    $meta = wp_get_attachment_metadata($post->ID);

    if ( !empty[$meta['width']] )
        if ( $meta['width'] <= 999 or $meta['height'] <= 349 ) 
            echo '<p class="error">Image dimensions ...bla</p>';
             // Return all form fields
             return $form_fields;

add_filter( 'attachment_fields_to_edit', 'wpse_attachment_dimension_check', 10, 2 );

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.


  • Thanks for the reply, your code does essentially what Maor Barazany's code does (which is linked in the question), although yours is much better. Commented Apr 12, 2012 at 13:08

Here is one way of making sure there is a properly sized thumbnail before displaying it.

First, create this helper function:

function has_post_thumbnail_of_size($width, $height) {
    $thumbnail_id = get_post_thumbnail_id();
    if( $thumbnail_id ) {
        $thumbnail_metadata = wp_get_attachment_metadata( $thumbnail_id );
        if( $thumbnail_metadata['height'] >= $height && $thumbnail_metadata['width'] >= $width ) {
            return true;
    return false;

Then you can now check before displaying the post thumbnail:

if( has_post_thumbnail_of_size(640, 360) ) {
  • I need to restrict a user from attaching an image that is too small, not restrict it from displaying if it is too small. Every post MUST include an image. Every post will display an image. Commented Apr 11, 2012 at 19:29
  • Would have to make tests but there is one way I see how this could be done: Whenever a upload was successful, WP will fetch the attachment data via ajax to display the attachment details form afterwards. This means the image has been inserted as an attachment and the metadata is available. IDEA: One could hook into attachments_fields_to_edit, which gets the attachment object as argument, then check the metadata height and width, if this check fails don't show the the attachment details form as usual, show a error message instead and delete the attachment instantly. just an idea. Commented Apr 11, 2012 at 22:56
  • @ungestaltbar, sounds like you have the right idea. I am no coder, so I am at the mercy of the community. Commented Apr 12, 2012 at 3:22

This will do whatcha need :)

//Adding script to deligate Thumbnail Size
if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 960, 276, true ); // default Post Thumbnail dimensions   
//Set different Thumbnail Sizes for Later
if ( function_exists( 'add_image_size' ) ) { 
  add_image_size( 'large-thumb', 960, 276, true ); //(cropped)  
  add_image_size( 'medium-thumb', 605, 174, true ); //(cropped) 
  add_image_size( 'small-thumb', 288, 83, true ); //(cropped) 
  add_image_size( 'small-square', 100, 100, true ); //(cropped) 

<?php if ( has_post_thumbnail() ) {
      global $post; //I usually define this in the function that outputs this, fyi
      echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
      echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
      echo '</a>'; 
      } else {

      $thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'rand','meta_key' => '_thumbnail_id'));
      foreach ($thumbnails as $thumbnail) {
      echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
      echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
      echo '</a>';

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 had add_image_size( 'small-square', 100, 100, true ); you could call on 'small-square' alternatively.


  • Thanks for the reply. This does not do what I need, however. The website is a MLB baseball website, so a random picture of Albert Pujols will not work to be added to an article about Prince Fielder, if you get what I mean... Also, as I mentioned above, I use the WyPiekacz plugin to force users to add a featured image. So, the issue is not whether they add an image, but instead that I want to force them to add an image that is larger than 640 wide and 360 tall. Commented Apr 12, 2012 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.