Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 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!

    Here’s my code that’s failing dismally… any suggestions?

    // in the WP loop
    
    $post_ID = get_the_ID();
    $content = get_the_content();
    image_transfer($content, $post_ID);
    
    // in my functions.php
    
    function image_transfer($string, $post_id){
    
    // use simple_html_dom to parse string and extract image url from img src
    $html = str_get_html($string);
    foreach($html->find('img') as $element) {
    
    // copied from acsnaterse code above
    $filepath = $element->src;
    $wp_filetype = wp_check_filetype( basename( $filepath ), null );
    $aFile["name"] = basename( $filepath );
    $aFile["type"] = $wp_filetype;
    $afile["tmp_name"] = $filepath;
    
    $attach_id = media_handle_sideload( $aFile, $post_id );
    
    // see what's going on...
    print_r($wp_filetype);
    echo '<br/><br/>';
    print_r($attach_id);
    
    }

    Thanks.

    I’d love some info on media_handle_sideload too! Any feedback?

    I’m having real problems getting media_handle_sideload to work. Does the filepath have to be relative? Surely it can also handle an absolute path?

    I’m trying to copy images from one website to another (upgrading a joomla site to a WP site while the joomla site stays live) and have extracted the URL’s to the images from the img tags using a Simple_Html_DOM parser.

    I think I should be able to feed these URL’s to media_handle_sideload and the function handles the image file transfer… but it won’t work!! I keep getting “[upload error] file is empty, try uploading something more substantial”.

    I’ve tried duplicating acsnaterse’s code but that doesn’t seem to change things.

    Many thanks for any suggestions.

    Hi Jan212 – Congrats on getting it to work.

    I’m doing an import from a really old Joomla into WP. Having copied all the posts (7000+) over, now I need to also copy their related images.

    Basically I just have a url to each image.

    I’ve just started looking at using media_handle_sideload but haven’t gotten it to work yet.

    Can you offer any tips on how you got it work?

    Many thanks!

Viewing 5 replies - 1 through 5 (of 5 total)