0

I've tried attach an image to post but my code doesn't work and I tried a lot of things, a lot of code, etc. What happen? I've tried with this:

            $postData = array(
              'post_title'    => 'My post',
              'post_content'  => '',
              'post_status'   => 'publish',
              'post_author'   => 1,
              'post_category' => array( 8,39 )
            );
            $post_id = wp_insert_post( $postData );

            $getImageFile = '/var/www/html/wp-content/uploads/pluginQ/test.jpg';

            $upload_dir = wp_upload_dir();
            $image_data = file_get_contents($getImageFile);
            $filename = basename($getImageFile);
            if(wp_mkdir_p($upload_dir['path'])){
              $file = $upload_dir['path'] . '/' . $filename;
            }
            else{
              $file = $upload_dir['basedir'] . '/' . $filename;
            }                                   
            file_put_contents($file, $image_data);

            $wp_filetype = wp_check_filetype($filename, null );
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => sanitize_file_name($filename),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
            wp_update_attachment_metadata( $attach_id, $attach_data );
            set_post_thumbnail( $post_id, $attach_id );

1 Answer 1

0

Hey I had the same problem about 4 5 days , until I found this code , try some tweaks and see if works :)

#Upload and Create attachment
        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');
        }
            $upload = wp_upload_bits( $_FILES['file']['name'], null, file_get_contents( $_FILES['file']['tmp_name'] ) );
            $wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
            $check = getimagesize($_FILES["file"]["tmp_name"]);
            if($check !== false) {
            $uploadOk = 1;
                } else {
                echo "File is not an image.";
            $uploadOk = 0;
                wp_trash_post( $post_id  );
            }

            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),    
                'post_content' => '',
                'post_author' => $user_id,
                'post_status' => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
            wp_update_attachment_metadata( $attach_id, $attach_data );
    #Upload and Create attachment

    #Use attachment
            update_post_meta( $post_id, '_thumbnail_id', $attach_id );
    #Use attachment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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