Site icon Hip-Hop Website Design and Development

Inserting picture into publish content material – Entrance Finish

That is the php I’ve for a kind which creates a publish with thumbnail. I wish to know how one can add the uploaded picture to the publish content material as effectively. Tried just a few issues, however they did not work.

 if(isset($_POST['new_post']) == '1') {

        $post_title = $_POST['post_title'];
        $post_category = $_POST['cat'];
        $filename = $_POST['attach'];

       $new_post = array(
          'ID' => '',
          'post_author' => $current_user->ID, 
          'post_category' => array($post_category),
          'post_title' => $post_title,
          'post_status' => 'draft'
        );

        $post_id = wp_insert_post($new_post);
        $publish = get_post($post_id);
        $new_post = $post->ID;

     if (!function_exists('wp_generate_attachment_metadata')){
            require_once(ABSPATH . "wp-admin" . '/consists of/picture.php');
            require_once(ABSPATH . "wp-admin" . '/consists of/file.php');
            require_once(ABSPATH . "wp-admin" . '/consists of/media.php');
            }

             if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "add error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $new_post );
            }   
            }

            if ($attach_id > 0){

            //and if you wish to set that picture as Put up  then use:
            update_post_meta($new_post,'_thumbnail_id',$attach_id);
            }

}