0

I have front-end user form like,

  • Title
  • Image Upload
  • Drop Down Category
  • Description
  • Tag

Now when a user fills the form and hits the publish button from the front-end..... at the back-end a post is created with title and all the details, im able to create a post with post title and the description, but im unable to save the category, tag. and how to add category and tags to the columns

enter image description here

For image upload i searched for solutions but nothing helped, the code is

<form id="new_post"<?php do_action('post_edit_form_tag'); ?> name="new_post" method="post" action="">

                    <!-- 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">Parent:</label><br />
                    <?php 
                        wp_dropdown_categories('type=product&show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=0&show_option_none=Select Gender&name=main_prod&taxonomy=product_cat');
                    ?>
                    </p>
                    <!-- Image Upload -->
                    <p><label for="description">Image Upload</label><br />
                    <input type="file" name="file" id="file" /> 
                    <br />

                    <!-- post Content -->
                    <p><label for="description">Content</label><br />
                    <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
                    </p>

                    <!-- post tags -->
                    <p><label for="post_tags">Tags:</label></br>
                    <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
                    <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>

                <?php
                    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'   => 'publish',           // Choose: publish, preview, future, draft, etc.
                         'post_type' => 'product'  //'post',page' or use a custom post type if you want to
                        ); 
                        //save the new post
                        $pid = wp_insert_post($new_post);
                        //insert taxonomies
                    }

in functions.php

/////////////////////////// file upload function ///////////////////////

add_action( 'post_edit_form_tag' , 'post_edit_form_tag' );

function post_edit_form_tag( ) {
    echo ' enctype="multipart/form-data"';
}

i dont want to use any plugins, any ideas plz

6
  • "i dont want to use any plugin" ... ugh. Isn't your code in a plugin or theme already... ?
    – kaiser
    Commented Jun 28, 2012 at 12:13
  • @kaiser no kaiser..... without any plugin involvement im writing the logic manually.....as my client doesn't want plugins. can this be done Commented Jun 28, 2012 at 12:18
  • 1
    I know you don't want to use a plugin. And you've probably done your research, but if you haven't seen this, Gravity Forms does what you want to do.
    – Joshc
    Commented Jun 28, 2012 at 12:29
  • "Plugin", "Dropins", "MU-Plugins", "Themes" is just an organizational wrapper for files. Nothing else. They got nothing to do with security, performance or anything. It's just the convention where to put your files. Period. So: WHERE are you putting above code?
    – kaiser
    Commented Jun 28, 2012 at 12:39
  • @kaiser i created a template page for user, so in the User template page im writing this code Commented Jun 28, 2012 at 12:42

2 Answers 2

2

Also, how are you processing your image upload? That function only sets the enctype.

By adding the following beneath your call to wp_insert_post you will be able to process your image upload,

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, $pid );
            add_post_meta($pid, 'meta_key_to_attach_image_to', $attach_id, false);
            continue;

        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($pid,'_thumbnail_id',$attach_id);

        }
    }
}
2
  • as you know WordPress does not have the enctype="multipart/form-data" attribute which is required for file uploading. So yeah in my code i didnt write logic for the uploaded image, can you guide me where to keep this <?php wp_generate_attachment_metadata( $attachment_id, $file ); ?> and what args should i pass in the function, kindly bare with me Commented Jun 28, 2012 at 12:48
  • Paste the snippet shown beneath your call to wp_insert_post, you do not need to include the line add_post_meta if you are not uploading the file to a meta field, however the update_post_meta function is necessary to receive the attachment ID processed by media_handle_upload so it can attach it to your post/thumbnail.
    – Adam
    Commented Jun 28, 2012 at 13:48
1

Add this after save new post:

//Set the object terms after when we have the ID
wp_set_post_terms( $new_post, $tags, 'post_tag' );

This will save your tags.

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.