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.

when i'm inserting a single picture thumbnail from the gallery with a link to the original picture, it gives me this:

<a href="http://www.dayadamy.org/wp-content/uploads/2011/12/PC220001.jpg"><img class="alignright size-general-thumb wp-image-2264" title="title of picture" src="http://www.dayadamy.org/wp-content/uploads/2011/12/PC220001-158x130.jpg" alt="alt of picture" width="158" height="130"></a>

but i want it to check if there is a smaller version of the large picture (like a 1024x1024 witch is an image_size of mine) and if there is echo this (pat attention to the file name at the end of the href attribute:

<a href="http://www.dayadamy.org/wp-content/uploads/2011/12/PC220001-1024x1024.jpg"><img class="alignright size-general-thumb wp-image-2264" title="title of picture" src="http://www.dayadamy.org/wp-content/uploads/2011/12/PC220001-158x130.jpg" alt="alt of picture" width="158" height="130"></a>

if there isn't a file in that size, echo the usual. anybody knows how do to this?

the reason i'm looking for it, is because i don't want my clients to start resizing images from his digital camera. up until now if i'm not resizing the picture the file will be maximum size, witch in most cases is 4600x3400 or something like this (ths is whats coming out from the digital cameras by default).

anyone? thanks.

share|improve this question
add comment

1 Answer

You can use:

<?php $image_attributes = wp_get_attachment_image_src( $attachment_id, array(1024,1024) ); ?>
<a href="<?php echo $image_attributes[0]; ?>">xxx</a>

See http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

If the picture is too small the original will show, otherwise cropped version (1024x1024).


Update: Didn't understand your question correct. Here is what you need:

First of all add a thumbnail size for your custom max size (true is hard crop, false is soft proportional crop)

add_image_size( 'custom_size', 1024, 1024, true );

Then add this also in your functions.php:

function custom_image_media_send_to_editor($html, $attachment_id, $attachment) {
    $attachment_ = wp_get_attachment_image_src( $attachment_id, 'custom_size' );
    $attachment['url'] = $attachment_[0];

    $post =& get_post($attachment_id);
    if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
        $url = $attachment['url'];
        $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
        $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
        $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
        $rel = ( $url == get_attachment_link($attachment_id) );

        return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
    }

    return $html;
}
add_filter('media_send_to_editor', 'custom_image_media_send_to_editor', 11, 3);

Then the next image you will add in your posts get an URL to the cropped version if the image is larger, or the full version is the image is smaller.

share|improve this answer
    
I can't do this, because i need it to be used with the standard WYSWYG image implementing procedure of wordpress. what you've suggested can work for manual implementation. –  Asaf Chertkoff Jan 5 '12 at 8:31
    
Updated the answer. Solution below the horizontal rule. –  Rob Vermeer Jan 5 '12 at 19:32
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.