WordPress - 如何防止表单重新提交?

时间:2016-07-07 12:07:35

标签: wordpress wordpress-4.5.2

如何在WordPress中阻止格式重新提交

我有一个带有表单的自定义页面:

<?php
/*
Template Name: Submission
*/

get_header(); ?>

    <!-- page article -->
    <?php
    // Include the page nav.
    get_template_part( 'nav-page', 'nav' );
    ?>

    <!-- container -->
    <div class="container">

        <!-- item -->
        <div class="col-md-4">

            <!-- content -->
            <div class="multimedia-submission-text-block">
                <?php
                // Start the loop.
                while ( have_posts() ) : the_post();

                    // Include the page content template.
                    get_template_part( 'content-page-article-submission', 'page psubmission' );

                // End the loop.
                endwhile;
                ?>
            </div>
            <!-- content -->

        </div>
        <!-- item -->

        <!-- form -->
        <div class="col-md-8">
            <?php
            // Set default variables.
            $anon_submission_name = '';
            $anon_submission_email = '';
            $anon_submission_biography = '';
            $success = false;
            $error = false;

            if (
                isset($_POST['anon_submission_form_create_post_submitted'])
                && wp_verify_nonce($_POST['anon_submission_form_create_post_submitted'],'anon_submission_form_create_post')
            ) {
                $anon_submission_name = trim($_POST['anon_submission_name']);
                $anon_submission_email = trim($_POST['anon_submission_email']);
                $anon_submission_biography = trim($_POST['anon_submission_biography']);

                if ($anon_submission_title != ''
                    && $anon_submission_name != ''
                    && $anon_submission_email != ''
                    && $anon_submission_biography != ''
                ) {
                    // post_author Take (int) The ID of the user who added the post. Default is the current user ID
                    $post_data = array(
                        'post_title' => $anon_submission_title,
                        // 'post_content' => $anon_submission_description,
                        'post_status' => 'pending',
                        'post_author' => 1, // admin user id
                        'post_type' => 'post'
                    );

                    // Insert the post and get its id.
                    $post_id = wp_insert_post($post_data);

                    if($post_id){
                        // Save other info in meta.
                        update_post_meta($post_id,'anon_submission_meta_name', $anon_submission_name);
                        update_post_meta($post_id,'anon_submission_meta_email', $anon_submission_email);
                        update_post_meta($post_id,'anon_submission_meta_biography', $anon_submission_biography);

                        $success = true;
                        echo '<p class="alert alert-success"><span class="close" data-dismiss="alert">&times;</span> Work submitted and awaiting moderation. Thank you for your submission.</p>';

                    }

                } else { // author or text field is empty

                    $error = true;
                    echo '<p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</span>  Work NOT submitted! Please correct the error(s) below.</p>';

                }
            }
            ?>

            <form class="form-submission" method="post" action="<?php echo $current_url . '?' . mt_rand(1, 1000); ?>">

            <?php wp_nonce_field('anon_submission_form_create_post', 'anon_submission_form_create_post_submitted'); ?>

            <div class="row">

                <!-- item -->
                <div class="col-md-6">

                    <div class="form-group<?php if ($error && empty($anon_submission_name)): ?> has-error<?php endif; ?>">
                        <label for="inputName">Name (required)</label>
                        <input type="text" class="form-control" id="input-name" placeholder="Name" name="anon_submission_name" value="<?php if ($error) echo $anon_submission_name; ?>">
                    </div>

                    <div class="form-group<?php if ($error && empty($anon_submission_email)): ?> has-error<?php endif; ?>">
                        <label for="inputEmail">Email Address (required)</label>
                        <input type="email" class="form-control" id="input-email" placeholder="Email" name="anon_submission_email" value="<?php if ($error) echo $anon_submission_email; ?>">
                    </div>

                    <div class="form-group<?php if ($error && empty($anon_submission_biography)): ?> has-error<?php endif; ?>">
                        <label for="inputMessage">Biography (required)</label>
                        <textarea class="form-control" rows="6" placeholder="Biography" name="anon_submission_biography"><?php if ($error) echo $anon_submission_biography; ?></textarea>
                    </div>


                </div>
                <!-- item -->

            </div>

            <button type="submit" class="btn btn-default no-gradient">Submit</button>

            </form>

        </div>
        <!-- form -->
    </div>
    <!-- container -->

<?php get_footer(); ?>

当您单击刷新按钮时,无论您是否提供了所有必填字段的信息,都会弹出“确认表单重新提交”。

有什么想法吗?

修改

重定向:

...
} else { // author or text field is empty

    wp_redirect( home_url() );

}

错误:

  

警告:无法修改标头信息 - 已发送的标头   (输出从/var/www/project-path/wp-includes/embed.php:363开始)   第1167行/var/www/project-path/wp-includes/pluggable.php

2 个答案:

答案 0 :(得分:1)

不要在模板文件中写下您的操作。在主题的函数中写下你的动作.php

add_action('init','your_function_name');

function your_function_name(){
 if($_POST['your_var']){
   //...... do what ever
   // then if neeeded do wp_redirect
 }
}

希望这可以解决您的问题。

答案 1 :(得分:0)

快速解决方案:

我把处理代码:

            <?php
            // Set default variables.
            $anon_submission_name = '';
            $anon_submission_email = '';
            $anon_submission_biography = '';
            $success = false;
            $error = false;
            ....

在:

<?php
/*
Template Name: Submission
*/

get_header(); ?>

然后在验证并发送表单后重定向:

// Redirect to prevent form resubmission.
wp_redirect(get_permalink($post->ID) . '?success=true');