2

I can get a list of urls for an image like this:

$sizes = array_merge(array('full'),get_intermediate_image_sizes());
foreach ($sizes as $imageSize) {
    $image_attributes = wp_get_attachment_image_src( $post->ID , $imageSize );
    echo   $imageSize . ':<br>' .$image_attributes[0].'<br>';
}

which gives me

full:
http://domain.com/wp-content/_uploads/2014/01/my_image.png
thumbnail:
http://domain.com/wp-content/_uploads/2014/01/my_image-150x150.png
medium:
http://domain.com/wp-content/_uploads/2014/01/my_image-300x116.png 

What I want is

full:
/path/to/public_html/wp-content/_uploads/2014/01/my_image.png 
thumbnail:
/path/to/public_html/wp-content/_uploads/2014/01/my_image-150x150.png 
medium:
/path/to/public_html/wp-content/_uploads/2014/01/my_image-300x116.png 
2

This should achieve what you want, however note that there will be backslashes AND forward slashes in the resulting path name. If this causes a problem on your web server you may need to do a further str_replace to forward slashes with backslashes

See comments in code for more info

$sizes = array_merge(array('full'),get_intermediate_image_sizes());
$uploads = wp_upload_dir();
foreach ($sizes as $imageSize) {
    // Get the image object
    $image_object = wp_get_attachment_image_src($post->ID,$imageSize );
    // Isolate the url
    $image_url = $image_object[0];
    // Using the wp_upload_dir replace the baseurl with the basedir
    $image_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $image_url );
    // echo it out
    echo   $imageSize . ':<br>' .$image_path.'<br>';
}
  • I think the backslashes will only be if youre on a Win (local dev) server. i usually do $_SERVER['DOCUMENT_ROOT'] = str_replace('\\','/',$_SERVER['DOCUMENT_ROOT']); in the index.php or config, etc, and never worry about it, after that. Will accept this, if no one comes up with a built in wp func. I couldnt find anything that does it, in wp. – Doug Cassidy May 14 '14 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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