未在前端帖子提交时重定向

时间:2012-01-26 作者:Towfiq

我一直在尝试此代码以提交前端帖子:http://pastebin.com/B8LGhsGA

单击“提交”按钮时,它不会重定向表单工作正常,但不会重定向。提交帖子后,它会再次加载相同的页面。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

那是因为你在打电话wp_redirect 从代码中获得一些缓冲区输出后。您应该在功能上更改模板的顺序,这意味着首先检查表单是否已提交,然后显示页面,因此请尝试以下操作:

<?php
/*
Template Name: Submit Work
*/
?>
<?php

// Check if the form was submitted
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] )) {

        // 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 = htmlentities(trim(stripcslashes($_POST[\'description\']))); 
    } else {
        echo \'Please enter the content\';
        }

        // Add the content of the form to $post as an array
        $type = trim($_POST[\'Type\']);
        $post = array(
                \'post_title\'    => $title,
                \'post_content\'  => $description,

                \'post_status\'   => \'pending\',                     // Choose: publish, preview, future, etc.
                \'post_type\'     => \'work\',  // Use a custom post type if you want to
                \'tax_input\'    => array( $type)
        );
        $post_id = wp_insert_post($post);
        wp_set_post_terms($post_id,$type,\'Type\',true);
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    $newupload = insert_attachment($file,$post_id);
                    // $newupload returns the attachment id of the file that
                    // was just uploaded. Do whatever you want with that now.
                }
            }
        wp_redirect( \'http://localhost/buddypress/thank-you\' );
        exit();
} // end IF

?>
<?php get_header() ?>

    <div id="content" class="two_column">
        <div class="padder">
  <h1 class="page_title">Works<span class="work_logo"></span></h1>      

        <?php do_action( \'bp_before_blog_page\' ) ?>

        <div class="page" id="blog-page" role="main">
        <h2 class="posttitle">Submit Work</h2>

<!--SUBMIT POST-->
<div id="postBox">


        <form id="new_post" name="new_post" class="post_work" method="post" enctype="multipart/form-data">
                <p><label for="title">Title</label><br />
                <input type="text" id="title" class="required" value="" tabindex="1" size="20" name="title" />
                </p>
                <p><label for="description">Description</label><br />
                <textarea id="description" type="text" class="required" tabindex="3" name="description" cols="50" rows="6"></textarea>
                </p>
                <p align="right"><input type="submit" value="Submit" tabindex="6" id="submit" name="submit" /></p>
                <p class="post_category"><label for="category">Category</label>
                <?php wp_dropdown_categories(\'taxonomy=Type&hide_empty=0&orderby=name&order=asc&name=Type\') ?></p>

                <div style="clear:both;"></div>
<p><label for="attachment">Photos: </label><input type="file" id="attachment">
<div id="attachment_list"></div></p>



                <input type="hidden" name="post_type" id="post_type" value="domande" />
                <input type="hidden" name="action" value="post" />

                <?php wp_nonce_field( \'new-post\' ); ?>
        </form>

</div>
<script>
    var multi_selector = new MultiSelector( document.getElementById( \'attachment_list\' ), 8 );
    multi_selector.addElement( document.getElementById( \'attachment\' ) );
</script>

<!--SUBMIT POST END-->

        </div><!-- .page -->

        <?php do_action( \'bp_after_blog_page\' ) ?>

        </div><!-- .padder -->
    </div><!-- #content -->

    <?php locate_template( array( \'sidebar-work.php\' ), true ) ?>

<?php get_footer(); ?>

SO网友:Rob Vermeer

有两件事,1 wp\\u redirect()在页面已经启动时不会被调用,因此您必须提前调用它。2 wp\\u redirect()需要退出;函数调用后。

wp_redirect( $location, $status );
exit;
你可以在这里读到http://codex.wordpress.org/Function_Reference/wp_redirect

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。