0

i use this code to create a page with the user can send a post on front-end but i have a problem..when i click "submit" nothing happen. Where wrong?

<?php
/*
Template Name: Rate Wine Form
*/
?>
<?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['submit'])) {
            $error = "";

        if (!empty($_POST['title'])) {
            $title = $_POST['title'];
     } else {
        $error .= "Please add a title<br />";
    }

        if (!empty($_POST['description'])) {
            $description = $_POST['description'];
     } else {
        $error .= "Please add a description<br />";
    }

        if (!empty($_POST['post_tags'])) {
            $post_tags = $_POST['post_tags'];
     } else {
        $error .= "Please add some keywords<br />";
    }

        if (!empty($_POST['winerating'])) {
            $post_tags = $_POST['winerating'];
     } else {
        $error .= "Please add some keywords<br />";
    }
        // IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
        if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                //Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)

                if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {

                    $tmpName = $_FILES[$file]['tmp_name'];
                    list($width, $height, $type, $attr) = getimagesize($tmpName);

                if($width!=630 || $height!=580)
                {
                    $error .= "Image is to small<br />";
                    unlink($_FILES[$file]['tmp_name']); 
                }

                // Get the type of the uploaded file. This is returned as "type/extension"
                $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                $uploaded_file_type = $arr_file_type['type'];

                 // Set an array containing a list of acceptable formats
                $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');

                 // If the uploaded file is the right format
                if(in_array($uploaded_file_type, $allowed_file_types)) {

                } else { // wrong file type
                $error .= "Please upload a JPG, GIF, or PNG file<br />";
                     }

                } else {
                $error .= "Please add an image<br />";
                }
            } // end for each
        } // end if

        $tags = $_POST['post_tags'];
        $winerating = $_POST['winerating'];

        // ADD THE FORM INPUT TO $new_post ARRAY
        if (empty($error)) {
            $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' =>  'post',  //'post',page' or use a custom post type if you want to
            'winerating'    =>  $winerating
        );

        //SAVE THE POST
        $pid = wp_insert_post($new_post);

        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
        wp_set_post_tags($pid, $_POST['post_tags']);

        //REDIRECT TO THE NEW POST ON SAVE
        $link = get_permalink( $pid );
        wp_redirect( $link );

        //ADD OUR CUSTOM FIELDS 
        add_post_meta($pid, 'rating', $winerating, true); 

            //INSERT OUR MEDIA ATTACHMENTS
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                $newupload = insert_attachment($file,$pid);
                // $newupload returns the attachment id of the file that
                    // was just uploaded. Do whatever you want with that now.
                }

            } // END THE IF STATEMENT FOR FILES
        } // END SAVING POST
    } // END VALIDATION
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

?>

<?php get_header(); ?>

        <div id="container">
            <div id="content" role="main">

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php if ( is_front_page() ) { ?>
                        <h2 class="entry-title"><?php the_title(); ?></h2>
                    <?php } else { ?>
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    <?php } ?>

                    <div class="form-content">
                     <?php
                        if (!empty($error)) {
                            echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
                        } elseif (!empty($success)) {
                            echo '<p class="success">' . $success . '</p>';
                        }
                    ?>
                        <?php the_content(); ?>

        <!-- WINE RATING FORM -->

        <div class="wpcf7">
        <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
            <!-- post name -->
            <fieldset name="name">
                <label for="title">Wine Name:</label>
                <input type="text" id="title" value="" tabindex="5" name="title" />
            </fieldset>

            <!-- post Category -->
            <fieldset class="category">
                <label for="cat">Type:</label>
                <?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
            </fieldset>

            <!-- post Content -->
            <fieldset class="content">
                <label for="description">Description and Notes:</label>
                <textarea id="description" tabindex="15" name="description" cols="80" rows="10"></textarea>
            </fieldset>

            <!-- wine Rating -->
            <fieldset class="winerating">
                <label for="winerating">Your Rating</label>
                <input type="text" value="" id="winerating" tabindex="20" name="winerating" />
            </fieldset>

            <!-- images -->
            <fieldset class="images">
                <label for="bottle_front">Front of the Bottle</label>
                <input type="file" name="bottle_front" id="bottle_front" tabindex="25" />
            </fieldset>

            <fieldset class="images">
                <label for="bottle_rear">Back of the Bottle</label>
                <input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
            </fieldset>

            <!-- post tags -->
            <fieldset class="tags">
                <label for="post_tags">Additional Keywords (comma separated):</label>
                <input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
            </fieldset>

            <fieldset class="submit">
                <input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" />
            </fieldset>

            <input type="hidden" name="action" value="new_post" />
            <?php wp_nonce_field( 'new-post' ); ?>
        </form>
        </div> <!-- END WPCF7 -->

        <!-- END OF FORM -->
                        <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                        <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                    </div><!-- .entry-content -->
                </div><!-- #post-## -->

<?php endwhile; // end of the loop. ?>

            </div><!-- #content -->
        </div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • do you have debugging enabled? do you get any errors? have you made any attempts to determine where it's failing? – Milo Nov 19 '14 at 18:21
  • Yes, debbuging mode is enabele but i not have any error. I try to select "Submin" without any complet fild and i have any error message and i try to Subit with every filed complete but nothing happen. – skifast Nov 19 '14 at 18:51
  • I've done that tutorial from voodoopress (voodoopress.com/how-to-post-from-your-front-end-with-no-plugin) before and it works just fine. Were you able to get it working before adding the image upload capabilities? – Stephen S. Nov 19 '14 at 20:07
0

Some variables are reserved by WordPress and can't be used in custom forms, like "name". Maybe you are trying to use one of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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