私は次のコードを持っています:
$wp_filetype = wp_check_filetype( basename( $image_url ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url']. '/' . basename( $image_url ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($image_url)),
'post_content' => '',
'post_status' => 'publish'
);
$attach_id = wp_insert_attachment( $attachment, $image_url );
すべて正常に動作しますが、添付された img タグの src 属性で予想される URL とは異なります。( wp-contents/uploads/2013/03/ の代わりに wp-contents/uploads )