WordPress.org

Ready to get started?Download WordPress

Forums

Set minimum height and width for featured image? (11 posts)

  1. Travis Pflanz
    Member
    Posted 2 years ago #

    I want to make sure all authors upload a featured image that can actually be used as a featured image on the homepage without being stretched.

    I am looking for a method to restrict an original image that is smaller than 640x360 to be used as featured image.

    I have found a snippet of code that restricts the image height and width of ALL uploaded images (http://wordpress.stackexchange.com/questions/28359/minimum-image-size-for-uploading), but this will not work because I want authors to be able to add additional, smaller images to the post, if they like.

    I prefer a snippet of code to add to the functions.php, but I am open to suggestions for different methods/plugins/functions code, if anyone has one.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Try:

    <?php if ( has_post_thumbnail() ) :
    $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), array( 640, 360 ) );
    if ( $image[1] >= 640 &&   $image[2] >= 360 ) echo get_the_post_thumbnail( $post->ID, array( 640, 360 ) );
    endif; ?>
  3. Travis Pflanz
    Member
    Posted 2 years ago #

    esmi, thanks for the reply.

    I am not sure this code works the way I want, however.

    I want to make sure authors add an image that is at least 640x360. If an author attaches an image as the "Featured Image" that is smaller, they will get an error and need to upload a new image.

  4. Travis Pflanz
    Member
    Posted 2 years ago #

    Anyone, please

  5. Travis Pflanz
    Member
    Posted 2 years ago #

    I can't be the first person to need/want something like this?

  6. Travis Pflanz
    Member
    Posted 2 years ago #

    Ok, if this is not possible, does anyone have a suggestion for how to handle a situation when a user adds an image that is small or does nto add an image at all?

  7. Travis Pflanz
    Member
    Posted 2 years ago #

    I have also added this question on Stack Exchange. Hopefully between the two sites someone will have a suggestion.

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    OK - the reason I never got back to you is that it seems you need to control the images at the upload point - which isn't something I've played with before. So I don't have an awful lot to contribute. Plus you want to use minimums whilst most people (and their code solutions) are trying to impose maximums.

    Sorry :-(

  9. Travis Pflanz
    Member
    Posted 2 years ago #

    Ya, your reply is the norm I have been hearing.

    The thing though, if a user uploads an image that is too small, it just looks odd in my space reserved for a 640x360 image.

    The hunt continues...

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    Is the layout dependant upon this image size? If not, what about outputting the CSS for the image block in the head of each page? That way you could possibly calculate the image dimensions & generate the necessary CSS dynamically.

  11. Travis Pflanz
    Member
    Posted 2 years ago #

    Not sure how I missed your reply. Yes, I want consistency throughout the posts, so all posts must have an image that is 640x360.

    GREAT NEWS!

    This task has been completed and I'm sure many WordPress multi-author websites will be ecstatic!

    The current method requires the use of the WyPiekacz plugin. In the settings, select "Post thumbnail (Featured image) is required:" then edit the wypiekacz.php file.

    Find:

    $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') );
                }

    Change 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') );
            }

    Change 640 and 360 above to fit your needs.

    I am currently working to add this in the settings of the plugin.

    I am not really a coder at all, so if someone has the time, I would love the help.

    THANKS - To Rajeev Vyas on wordpress.stackexchange.com for his help!

Topic Closed

This topic has been closed to new replies.

About this Topic