try $EM_Event->image_upload();
you can see this under classes/em-event.php ( function save() )
to retrieve: something like this – http://pastebin.com/h0e8qCwu
maybe this csv importer will help you:
http://em.cm/importer-exporter
Thanks for the replies. I wasn’t yet able to solve this yet. To clarify, what I’d like to do is something like:
$EM_Event->image_upload( http://image.url/on/a/remote/server.jpg );
$EM_Event->save();
Unfortunately EM_Object->image_upload doesn’t take arguments. How to have EM retrieve and save an image from a remote server to an event?
Marcus: The CSV importer didn’t seem to have any image functionalities yet, but will likely prove useful even before they are added. Thanks!
You’ll need to add an image to the $_FILES global. WP doesn’t accept featured images to be external URLs, so you need to import it as if it was uploaded by a form (which is what our get_post and save functions deal with).
You may need to import it and save it directly as a WP attachment post type and then assign it to your event post type as the featured image.
Got it working with the following. Credits to Sutherland Boswell’s Video Thumbnails 1.8.2.
$new_thumbnail = $remote_image_url;
$EM_Event->save();
$post_id = $EM_Event->post_id;
// Return the new thumbnail variable and update meta if one is found
if ( ! empty( $new_thumbnail ) && ! empty( $post_id ) ) {
$response = wp_remote_get( $new_thumbnail, array( 'sslverify' => false ) );
// Check for error
if ( is_wp_error( $response ) ) {
return new WP_Error( 'thumbnail_retrieval', __( 'Error retrieving a thumbnail from the URL <a href="' . $new_thumbnail . '">' . $new_thumbnail . '</a> If opening that URL in your web browser shows an image, the problem may be related to your web server and might be something your server administrator can solve. Error details: "' . $response->get_error_message() . '"' ) );
}
$image_contents = wp_remote_retrieve_body( $response );
$upload = wp_upload_bits( basename( $new_thumbnail ), null, $image_contents );
//$new_thumbnail = $upload['url']; // The local URL, currently unused 2013-07-13
$filename = $upload['file'];
$wp_filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => get_the_title( $post_id ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
// you must first include the image.php file for wp_generate_attachment_metadata() to work
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}