samedi 31 janvier 2015

Access the resized images generated by `add_image_size()`


I have set up some shortcodes to make things simpler for the user. It wraps up some bootstrap markup and just lets you put the h1, some img urls, etc in it without getting too close to the markup.


What I originally wanted was to let the user upload the image through the media library and then grab the image url for the resize that I had generated. I thought this used to be in there but I can't find a way.


So assuming thats not possible, is there a way I can take a url such as http://ift.tt/1Dr14Pr which the user will copy paste into the shortcode and then use a WP function to get the url for the columns-thumbnail resize?


If its not built in I can just check the dimensions directly but it feels brittle to do it this way should the size change or the way WP generates filenames change in the future.


Some code:



function roots_setup() {
// snip

// setup image sizes
// http://ift.tt/xBzWPI
add_image_size('triple-thumbnail', 370, 222, true); // true = hard crop
add_image_size('columns-thumbnail', 200, 200, true);

// snip
}
add_action('after_setup_theme', 'roots_setup');

function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'triple-thumbnail' => __( 'Triple Header Image' ),
'columns-thumbnail' => __( 'Columns Thumbnail' )
) );
}
add_filter( 'image_size_names_choose', 'my_custom_sizes' );


Shortcode example:



[column imageurl='http://ift.tt/1Dr14Ps' title='Vertical Blinds']


Something that I'm imagining might be in WP already somewhere:



$imageurl = 'http://ift.tt/1Dr14Ps';
$thumbnailurl = WP_get_image_resize_url($imageurl, 'columns-thumbnail');


Does this exist?





Aucun commentaire:

Enregistrer un commentaire