Herşey sorunsuzca çalışıyor ancak resimler konu içerisine bir türlü girmiyor

.
dizinimde resimleri görüyorum ama ben istiyorum ki çektiğim konudaki resim konuya eklensin.
$resim = preg_match_all('# <img src="(.*?)" alt="(.*?)" title="(.*?)" width="(.*?)" height="(.*?)" />#',$site,$resimlera);
$resimler = "";
foreach($resimlera[1] as $resimo)
{
$resimm = str_replace("-85x70","",$resimo);
$resimler[] = $resimm;
}
foreach($resimler as $resim) {
$cm++;
$resim = trim($resim);
$url = $resim;
$pid = $post_id;
$bas = seflink($title)."-".$cm.".jpg";
$filename = $bas;
$filename = remove_accents($filename);
if (function_exists('mb_strtolower')) {
$filename = mb_strtolower($filename, 'UTF-8');
}
$filename = utf8_uri_encode($filename);
$filetype = wp_check_filetype($url);
extract($filetype);
if (!$type) $type = "image/jpeg";
$upload = wp_upload_bits($filename, $filetype, curl($url));
if ( !empty($upload['error']) ) {
die("Resim eklenemedi!<br />Hata: ".$upload['error']."<br />");
} else {
$neyim = $upload['file'];
$attachment = array(
'guid' => $upload['url'],
'post_mime_type' => $type,
'post_title' => $bas,
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $pid
);
$attach_id = wp_insert_attachment($attachment, $neyim, $pid);
$attach_data = wp_generate_attachment_metadata($attach_id, $neyim);
wp_update_attachment_metadata($attach_id, $attach_data);
if ($cm == 1)
add_post_meta($pid, '_thumbnail_id', $attach_id, true);
}
$gallery[] = $attach_id;
}
if (count($gallery) > 0) {
$posto = array(
'ID' => $post_id,
'post_content' => '[gallery ids="'.implode(",", $gallery).'"]',
'post_author' => 1);
wp_update_post($posto);
unset($gallery);
}
$resimler="";
$cm=0;