How to add image in submit post form for my guest uploads
-
Hi guys, can you help me on this. i created a submit form post. I was able to upload an image, the thing is, it is not attaching to the post submitted. here’s my code.
<?php /* Template Name: Submit Post Form */ get_header(); if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") { // Do some minor form validation to make sure there is content if (isset ($_POST['title'])) { $title = $_POST['title']; } else { echo 'Please enter a title'; } if (isset ($_POST['description'])) { $description = $_POST['description']; } else { echo 'Please enter the content'; } $tags = $_POST['post_tags']; // Add the content of the form to $post as an array $new_post = array( 'post_title' => $title, 'post_content' => $description, 'post_category' => array($_POST['cat']), // Usable for custom taxonomies too 'tags_input' => array($tags), 'post_status' => 'draft', // Choose: publish, preview, future, draft, etc. 'post_type' => 'great_images' //'post',page' or use a custom post type if you want to ); 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, $new_post ); } } if ($attach_id > 0){ //and if you want to set that image as Post then use: set_post_thumbnail($new_post,'_thumbnail_id',$attach_id); } //save the new post $pid = wp_insert_post($new_post); wp_redirect(get_permalink($pid)); exit; //insert taxonomies } ?> <div id="wrap"> <div id="header"> <?php get_template_part('logo'); get_template_part('nav'); ?> </div> <div id="content"> <div id="main"> <?php get_template_part('searches'); ?> <div class="single-post-item"> <h1>Ask a Question</h1> <div class="post-meta">Fill out the fields below, all of them are required!</div> <div class="inner-content"> <!-- New Post Form --> <div id="postbox"> <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data"> <!-- post name --> <p><label for="title">Title</label><br /> <input type="text" id="title" value="" tabindex="1" size="20" name="title" /> </p> <!-- post Category --> <p><label for="Category">Category:</label><br /> <p><?php wp_dropdown_categories( 'tab_index=3&taxonomy=category' ); ?></p> <!-- post Content --> <p><label for="description">Content</label><br /> <textarea id="description" tabindex="4" name="description" cols="50" rows="6"></textarea> </p> <!-- post tags --> <p><label for="post_tags">Tags:</label> <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p> <input type="file" name="thumbnail" id="thumbnail"> <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p> <input type="hidden" name="action" value="new_post" /> <?php wp_nonce_field( 'new-post' ); ?> </form> </div> </div> </div> </div> <?php get_sidebar(); ?> </div> </div> <?php get_footer(); ?>
- The topic ‘How to add image in submit post form for my guest uploads’ is closed to new replies.