By using the code below, I can create a new post and upload an image to wordpress’s upload
directory as a attachment through a form’s submitted data.
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(3)
);
$post_id = wp_insert_post($new_post);
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $post_id );
}
}
I want to insert an image to post, not replace the featured image so set_post_thumbnail()
function is not appropriate. I found that if I manually insert an image to post, it will generated the HTML code likes:
<a href="http://192.168.1.12/wp/wp-content/uploads/2012/09/H530U_08.jpg"><img class="alignnone size-medium wp-image-136" title="HARP_SH530U_0" src="https://192.168.1.12/wp/wp-content/uploads/2012/09/HARP_SH530U_08-300x167.jpg" alt="" width="300" height="167" /></a>
So which API would provide the function of inserting image to post, likes generating the HTML code above and not replacing the featured image?
Thanks