0

I have a situation where I am pulling images from an exterior wordpress blog, which also happens to have images. I am able to pull the links and titles of the latest posts, as well as the featured images. The issue is the images don't retrieve 100% of the time when being referenced from an external URL, so I wrote a script to run every midnight to pull the featured images, upload them to wordpress, then show those images. This worked perfectly in December, but I am testing it now and it is instead copying over the image, but with a numerical addition of -1 or -2 at the end, as if the image was uploaded previously. This is the code I have running every midnight.

 $response = wp_remote_get(    add_query_arg( array(
'per_page' => 5
), 'https://EXTERNAL-SITE/wp-json/wp/v2/posts' ) );

if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {

$remote_posts = json_decode( $response['body'] ); // our posts are here
foreach( $remote_posts as $remote_post ) {

    $featuredpix  = $remote_post->fimg_url;
    //$featuredpix  = str_replace(".jpg", "-520x346.jpg", $featuredpix);
    $picpath      = str_replace("https://EXTERNAL-SITE",$_SERVER["DOCUMENT_ROOT"],$featuredpix);
    $picname      = basename($featuredpix);

    global $wpdb;
    $image_src = wp_upload_dir()['baseurl'] . '/' . _wp_relative_upload_path( $picname );
    $query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE guid='$image_src'";
    $count = intval($wpdb->get_var($query));

    if (file_exists($picpath)) {

    } else {

        if( !class_exists( 'WP_Http' ) )
            include_once( ABSPATH . WPINC . '/class-http.php' );
            $http = new WP_Http();
            $response = $http->request( $featuredpix );
            if( $response['response']['code'] != 200 ) {
                return false;
            }
            $upload = wp_upload_bits( basename($featuredpix), null, $response['body'] );
            if( !empty( $upload['error'] ) ) {
                return false;
            }
            $file_path = $upload['file'];
            $file_name = basename( $file_path );
            $file_type = wp_check_filetype( $file_name, null );
            $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
            $wp_upload_dir = wp_upload_dir();
            $post_info = array(
                'guid'           => $wp_upload_dir['url'] . '/' . $file_name,
                'post_mime_type' => $file_type['type'],
                'post_title'     => $attachment_title,
                'post_content'   => '',
                'post_status'    => 'inherit',
            );
            // Create the attachment
            $attach_id = wp_insert_attachment( $post_info, $file_path );
            // Include image.php
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            // Define attachment metadata
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
            // Assign metadata to attachment
            wp_update_attachment_metadata( $attach_id,  $attach_data );
            return $attach_id;
    };


}
} 

Your Answer

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

Browse other questions tagged or ask your own question.