• 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] => https://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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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!

    Hey folks, you’re pretty much right!

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

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

    That might be the cause… in that case, using a plugin like https://www.ads-software.com/extend/plugins/ajax-thumbnail-rebuild/ might fix the issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_get_attachment_image_src problem’ is closed to new replies.