function insert_image2post( $postid, $image, $featured = false ) {
global $wpdb;
$post_title = $wpdb->get_var("SELECT $wpdb->posts.post_title FROM $wpdb->posts WHERE ID = '$postid'");
if( is_null( $post_title ) )
return false;
$post_name = sanitize_title( $post_title );
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$photo = new WP_Http();
$photo = $photo->request($image);
if( $photo['response']['code'] != 200 )
return false;
$attachment = wp_upload_bits( $post_name . '.jpg', null, $photo['body'], date("Y/m") );
if( !empty( $attachment['error'] ) )
return false;
$filetype = wp_check_filetype( basename( $attachment['file'] ), null );
$postinfo = array(
'post_mime_type' => $filetype['type'],
'post_title' => $post_title,
'post_content' => '',
'post_excerpt' => '',
'post_status' => 'inherit'
);
$filename = $attachment['file'];
$attach_id = wp_insert_attachment( $postinfo, $filename, $postid );
if( !function_exists( 'wp_generate_attachment_data' ) )
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 );
if( $featured )
add_post_meta($postid, '_thumbnail_id', $attach_id);
return array($attach_id, $attachment['url']);
}
Hocam yapamadım bu şekilde, elimde post ID ve resim linki var kendi sunucuma eklenmiş olarak ama beceremedim