Minimum Dimensions Requirement for Featured Image?

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.

Read More

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.

Related posts

Leave a Reply

6 comments

  1. 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_proper_dimension=true;
                   }
            }
    
            $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”.

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

  3. 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>';
                exit;
            }
            else
            {
                 // 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.

    cheers

  4. 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))
        return;
    
      $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" );
      if(!$image_data)
        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;
        wp_update_post(array(
          '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).

  5. 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) ) {
        the_post_thumbnail();
    }
    
  6. 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.

    Cheers