1

I'm encountering a weird issue when getting featured image urls with wp_get_attachment_image_src. If no featured image has been set, this should return null. However, in this case its returning wp-includes/images/crystal/default.png.

The code I'm using to get the URL is:

$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id,'full', true);
$thumb_url = $thumb_url_array[0]

This is happening in WordPress 4.1.

2
  • 1
    Have you tried disabling all plugins and using a default theme?
    – kraftner
    Jan 23, 2015 at 11:19
  • Yes. It happens in Twenty Fourteen with all plugins disabled. Jan 23, 2015 at 11:30

1 Answer 1

5

here's the wp_get_attachment_image_scr function:

function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {

    // get a thumbnail or intermediate image if there is one
    if ( $image = image_downsize($attachment_id, $size) )
        return $image;

    $src = false;

    if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
        /** This filter is documented in wp-includes/post.php */
        $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
        $src_file = $icon_dir . '/' . wp_basename($src);
        @list($width, $height) = getimagesize($src_file);
    }
    if ( $src && $width && $height )
        return array( $src, $width, $height );
    return false;
}

as u can see if 3-rd param is true then it looks in icons dir for that image ( and i assume wp_mime_type_icon returns default.png and some of your plugins add_filter to icon_dir to be wp-includes/images/crystal

Just remove 3-rd parameter in your example: $thumb_url_array = wp_get_attachment_image_src($thumb_id,'full'); and it should return false if no image or image url

1
  • 1
    Ah Thanks! It was right in front of my face in the Codex as well. Jan 23, 2015 at 11:34

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.