php - How do I link to external images in my WP posts -
i scraping site , putting data in wp site.
there lots of images in posts.
i want when importing scraped data wp site, want link images site directly external url instead of downloading image server.
i grammatically importing scraped data site.
here part of adding image
// image $ins_q = "insert wpxw_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, " . "comment_status, ping_status," . "post_password, post_name, to_ping, pinged, post_modified,post_modified_gmt, post_content_filtered,post_parent," . "guid,menu_order,post_type," . "post_mime_type,comment_count) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; $sth = $conn->prepare($ins_q); if (!$sth->execute(array( $post_author, $date, $date, "", sanitize_title_with_dashes($image_name), "", "inherit", "open", "closed", '', str_replace(".", "-", $image_name), '', '', $date, $date, '', $post_id, "http://photos2.zillowstatic.com/p_f/isdc6hruhctopo0000000000.jpg" , 0, "attachment", "image/jpeg", 0 ))) { echo "<h1>error</h1>"; print_r($sth->errorinfo()); exit(); }
}
my problem when adding external link image, images not show on post. how can show images external resources?
// add featured image post $image_url = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // define image url here $upload_dir = wp_upload_dir(); // set upload folder $image_data = file_get_contents($image_url); // image data $filename = basename($image_url); // create image file name // check folder permission , define file location if( wp_mkdir_p( $upload_dir['path'] ) ) { $file = $upload_dir['path'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; } // create image file on server file_put_contents( $file, $image_data ); // check image file type $wp_filetype = wp_check_filetype( $filename, null ); // set attachment data $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name( $filename ), 'post_content' => '', 'post_status' => 'inherit' ); // create attachment $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); // include image.php require_once(abspath . 'wp-admin/includes/image.php'); // define attachment metadata $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); // assign metadata attachment wp_update_attachment_metadata( $attach_id, $attach_data ); // , assign featured image post set_post_thumbnail( $post_id, $attach_id );
Comments
Post a Comment