WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_attachment_image_src problem (4 posts)

  1. petershep
    Member
    Posted 2 years ago #

    Hello,

    I'm have a problem with the wp_get_attachment_image_src function returning the incorrect url for an image.

    My code looks like this:

    $image_main_ar = wp_get_attachment_image_src( $image_ID, 'product_image_main' );

    product_image_main is a registered image size:

    add_image_size( 'product_image_main', 334, 385, false );

    but when I print the $image_main_ar array I get:

    Array (
    [0] => http://www.website.com/site/wp-content/uploads/2011/07/Screen-shot-2011-07-27-at-15.03.07.png
    [1] => 334
    [2] => 226
    [3] => )

    which shows the url for the original image I uploaded, not the resized version, and I've checked the resized version is in the upload folder.

    The weird thing is the fourth key in the array [3] is empty, but on the images where it returns the correct url it is set to '1'. I've looked in the docs but can't find anything about this fourth value.

    Does anyone have any ideas?

    Many thanks for any advice.

  2. touchy finger
    Member
    Posted 2 years ago #

    I had similar problems with wp_get_attachment_image_src, started pulling my hair out!

    It would appear, and I'm reluctant to say this being a relative amateur, that Codex is wrong... or something weird is going on.

    As you say, if you var_dump(wp_get_attachment_image_src($post->ID, $size)) you get an array with four values. Whereas Codex says you should only get only three.

    Codex says you get:

    array(
    [0] => image URL
    [1] => width
    [2] => height
    )

    And a boolean FALSE on failure.

    Whereas both yours and my results give a four value array.

    array(
    [0] => image URL
    [1] => width
    [2] => height
    [3] => boolean[true/false]
    )

    Just like you I was feeding wp_get_attachment_image_src a previously designated custom image size using add_image_size().

    In instances where that custom image size didn't exist for that image attachment (i.e. older posts published before I started using custom image size) wp_get_attachment_image_src still returns the array but with the fullsize image URL - BUT value 4 (key value 3) is false!

    So to check if your retrieval of a custom image size is successful you have to:

    $image_main_ar = wp_get_attachment_image_src( $image_ID, 'product_image_main' );
    if(!$image_main_ar[3]) { do something else ... maybe:
    $image_main_ar = wp_get_attachment_image_src( $image_ID );  }

    Hope that makes sense!

  3. brasofilo
    Member
    Posted 2 years ago #

    Hey folks, you're pretty much right!

    I agree that the Codex is wrong... Will investigate further and take some action upon...

  4. alesub
    Member
    Posted 2 years ago #

    Did the image size declaration existed before you uploaded that file?

    That might be the cause... in that case, using a plugin like http://wordpress.org/extend/plugins/ajax-thumbnail-rebuild/ might fix the issue.

Topic Closed

This topic has been closed to new replies.

About this Topic