Error creating post: ‘ . esc_html($post_id->get_error_message()) . ‘
‘; } else { // 2. Handle image upload if (!empty($_FILES[‘user_image’][‘name’]) && $_FILES[‘user_image’][‘error’] === 0) { require_once(ABSPATH . ‘wp-admin/includes/image.php’); require_once(ABSPATH . ‘wp-admin/includes/file.php’); require_once(ABSPATH . ‘wp-admin/includes/media.php’); $file = $_FILES[‘user_image’]; // Basic validation – only images $allowed = array(‘jpg|jpeg|jpe’ => ‘image/jpeg’, ‘png’ => ‘image/png’, ‘gif’ => ‘image/gif’); $file_type = wp_check_filetype(basename($file[‘name’]), $allowed); if (!empty($file_type[‘ext’])) { $upload_overrides = array(‘test_form’ => false); $movefile = wp_handle_upload($file, $upload_overrides); if ($movefile && !isset($movefile[‘error’])) { // Insert attachment $attachment = array( ‘post_mime_type’ => $file_type[‘type’], ‘post_title’ => sanitize_file_name(basename($file[‘name’])), ‘post_content’ => ”, ‘post_status’ => ‘inherit’, ‘post_author’ => get_current_user_id(), ); $attach_id = wp_insert_attachment($attachment, $movefile[‘file’], $post_id); if (!is_wp_error($attach_id)) { $attach_data = wp_generate_attachment_metadata($attach_id, $movefile[‘file’]); wp_update_attachment_metadata($attach_id, $attach_data); // Set as featured image set_post_thumbnail($post_id, $attach_id); $message = ‘Success! New post created (ID: ‘ . $post_id . ‘).
View your post (currently in draft)
Could not create attachment.
‘; } } else { $message = ‘Upload error: ‘ . esc_html($movefile[‘error’]) . ‘
‘; } } else { $message = ‘Please upload a valid image (jpg, png, gif only).
‘; } } else { $message = ‘Please upload an image.
‘; } } } ?>Upload Your Picture & Text → Create Post
Note: New posts are created as draft. You (or admin) can publish them later from wp-admin.