gera3d
2/25/2015 - 2:01 PM

[Really Simple CSV Importer] Example: import image from url to custom field

[Really Simple CSV Importer] Example: import image from url to custom field

<?php
/*
Plugin Name: Get Remote Image to Custom Field
*/

add_filter('really_simple_csv_importer_post_saved', function($post)
{
    if (is_object($post)) {
        // Get the meta data of which key is "image"
        $image = $post->image;

        // Create a instance of helper class
        $h = RSCSV_Import_Post_Helper::getByID($post->ID);

        // Get the remote image data
        $file = $h->remoteGet($image);

        // Then, attach it
        $attachment_id = $h->setAttachment($file);

        // Finally, replace the original data with the attachment id
        $h->setMeta( array( 'image' => $attachment_id ) );
    }
});