Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Can't answer your question, but i have to say thank you for a nice collection of handy filters and functions ;) –  Ole Henrik Skogstrøm Mar 27 '12 at 2:08
    
ditto, Ole Henrik :) –  brasofilo Mar 30 '12 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? –  Chip Bennett Apr 12 '12 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. –  Travis Pflanz Apr 12 '12 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? –  Chip Bennett Apr 12 '12 at 21:21
show 1 more comment

5 Answers

up vote 1 down vote accepted
+50

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

share|improve this answer
    
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/… –  Travis Pflanz Apr 12 '12 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. –  Travis Pflanz Apr 12 '12 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...... –  Rajeev Vyas Apr 13 '12 at 7:21
    
Thanks for all your help Rajeev! –  Travis Pflanz Apr 14 '12 at 13:39
add comment

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

share|improve this answer
    
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. –  Travis Pflanz Apr 12 '12 at 13:08
add comment

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

share|improve this answer
    
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. –  Travis Pflanz Apr 12 '12 at 3:20
add comment

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();
}
share|improve this answer
    
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. –  Travis Pflanz Apr 11 '12 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. –  ungestaltbar Apr 11 '12 at 22:56
    
@ungestaltbar, sounds like you have the right idea. I am no coder, so I am at the mercy of the community. –  Travis Pflanz Apr 12 '12 at 3:22
add comment

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

share|improve this answer
    
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. –  Travis Pflanz Apr 5 '12 at 20:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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