How to insert an image to a post(Addon Help)
===

So I have this addon which automates the process of posting news from several other news websites, which actually allows republishing.

This plugin is "FeedWordPress"

I am using "YD Feedwordpress Content Filter" for filtering out the specific keywords on which the news are to be fetched. I edited this plugin to fetch the url of the first image from google search search.

I am also able to put upload this image to the post. But I don't know how to insert it to the post. Can anybody help me?

I am not giving the plugin source code here. I have given the changes I made along with the part which is responsible for updating the post.

<pre>$imgsrc = $matches[1];
$title_moded=str_replace(" ", "+", $title);
$json_for_imgupload  = file_get_contents("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=".$title_moded);;
$json_for_imgupload = json_decode($json_for_imgupload);
$imgurl_upload=$json_for_imgupload->responseData->results[0]->url;
				//if( preg_match( '/\.(gif|jpeg|jpg|png)$/i', $imgurl_upload, $matches ) ) {
					$imgext = strtolower( $matches[1] );
					require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
					$snoopy = new Snoopy();
					$result = $snoopy->fetch( $imgurl_upload );
					if( $result ) {
						$upload_dir = wp_upload_dir();
						//$filename = sanitize_title( $title );
						//$filename = preg_replace( '/[^a-zA-Z0-9_-]/', '-', $filename );
$filename = rand();
$filename .= '.jpg';
						$outfile = $upload_dir['path'] . '/' . $filename;

						if( strlen( $snoopy->results ) > 512 && !file_exists( $outfile ) ) {
							if( $h = fopen( $outfile, 'w' ) ) {
								fwrite( $h, $snoopy->results );
								fclose( $h );
							} else {
								//echo 'File write error!<br>';
							}
							$outurl = $upload_dir['url'] . '/' . $filename;
							$stripped_content = str_replace( $imgsrc, $outurl, $stripped_content );
							
							// taken from codex doc @ http://codex.wordpress.org/Function_Reference/wp_insert_attachment
							$wp_filetype = wp_check_filetype( basename( $outfile ), null );
							$attachment = array(
								'post_mime_type' => $wp_filetype['type'],
								'post_title' => $title,
								'post_content' => '',
								'post_status' => 'inherit'
							);
							$attach_id = wp_insert_attachment( $attachment, $outurl ); // no post_id :-(
							// you must first include the image.php file
							// for the function wp_generate_attachment_metadata() to work
							require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
							$attach_data = wp_generate_attachment_metadata( $attach_id, $outfile );
							wp_update_attachment_metadata( $attach_id,  $attach_data );
						}
					}</pre>

