4

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="http://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

1
  • i think what you have to do is generate the neccessary html & append it to the post_content used in wp_insert_post, then use wp_update_post to update the new content Oct 18, 2012 at 9:03

1 Answer 1

5

You can set the uploaded image as the post thumbnail via,

update_post_meta($post_id,'_thumbnail_id',$attach_id);

Example...

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 );
    }   
}

update_post_meta($post_id,'_thumbnail_id',$attach_id);

If you are uploading an array of images, then you might wish to specify which image by referencing the key,

$file[0] //for the first image in the array
$file[1] //for the second image in the array
$file[2] //for the third image in the array
etc...

...but that should not be necessary when uploading a single image.

Update:

Based on your comments, if you want to insert the image at the beggining of or before your post content its probably best you filter the the_content function like so,

add_filter( 'the_content', 'insert_img_to_post', 20 );

function insert_img_to_post( $content ) {

    global $post;
    $post_id = $post->ID;

    if ( is_single() )

        $content = sprintf(
            '<img class="post-icon" src="%s" alt="Post icon" title=""/>',
            wp_get_attachment_url(get_post_meta( $post_id, '_thumbnail_id', true )),
            $content
        );

    return $content;
}

...add this function as a separate function/filter in your functions.php file. Do not try and bundle it within your wp_insert_post function as it will not work. This is intended to run by itself, hooking onto the_content filter, inserting the image at the top of the post before the content, then appending the content beneath it.

This is currently running on a single post page (is_single) if you want it to run on something else, like a page, then change to is_page or any other conditional tag that meets your needs.

12
  • Thanks for the code, I test that if I use your code, there are none image insert to post; On the other hand, if I put update_post_meta under the line of $attach_id, I will replaced the featured image. Oct 18, 2012 at 9:14
  • Yeah, sorry I just re-read, I thought you wanted to set the featured image of the post. But rather, you want to insert the image into the body of the content right?
    – Adam
    Oct 18, 2012 at 9:15
  • Yes, you are right, I want to insert the image into the body of the content. Oct 18, 2012 at 9:17
  • Where abouts? The begining, the end or somewhere in the middle (anywhere in the middle, i.e. amongst text).
    – Adam
    Oct 18, 2012 at 9:17
  • I think inserting the image to the begining of the content. Thanks Oct 18, 2012 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.