Send/Publish a Post front end

时间:2014-11-19 作者:skifast

我使用此代码创建一个页面,用户可以在前端发送帖子,但我有一个问题。。当我单击“提交”时,什么都没有发生。哪里错了?

<?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(); ?>

1 个回复
SO网友:Pablo Gavilan

有些变量是WordPress保留的,不能在自定义表单中使用,如“name”。也许你正在尝试使用其中一个。

结束

相关推荐

Page listing Custom Posts

我有自定义帖子(实际上是页面),可以在默认页面(page home.php)上查看,但如何创建一个单独的页面,包含完全相同的内容,并链接到它?主页。php(默认WP页)代码当前为:<?php /* Template Name: Home News */ ?> <?php get_header(); ?> <div id=\"content\"> <div id=\"inner-cont