0

I have a front end form users use to create custom posts. It works fine with Chrome and FF, but when in Safari two posts are created.

The code i'm using is below. I've tried a few conditionals shown in other answers but none worked. How can I prevent wp_insert_post from creating duplicate posts when submitted from Safari?

Thanks

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

$tags = $_POST['post_tags'];

$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_category' => array($_POST['cat']),
    'tags_input'    => array($tags),
    'post_status'   => 'publish',etc.
    'post_type' => 'website'
);

global $current_user;
get_currentuserinfo();

$user_name = strtolower($current_user->user_login);  //custom caps will default to lowercase anyway
$pid = wp_insert_post($new_post);

update_post_meta($pid, "s2member_ccaps_req", $user_name, true);

wp_redirect( get_permalink($pid) ); 
exit();

The form

                <form id="new_post" 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">Category:</label><br />
                    <p><?php wp_dropdown_categories( 'tab_index=3&taxonomy=category' ); ?></p>


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

                    <!-- post tags -->
                    <p><label for="post_tags">Tags:</label>
                    <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>
7
  • There's no way this can be browser related. The save takes place on the server - which browser was used to submit the form isn't relevant.
    – vancoder
    Commented Aug 12, 2013 at 20:21
  • @vancoder it's only happening in Safari
    – Anagio
    Commented Aug 12, 2013 at 20:36
  • Have you checked the value that's being returned into $pid to make sure it's a valid ID before you call update_post_meta? Commented Aug 12, 2013 at 21:17
  • @MorganEstes I just tested with var_dump($pid); it returned int(280) after trying to submit another post it returned int(282) the posts are being created just fine, it's just from Safari two posts are created
    – Anagio
    Commented Aug 12, 2013 at 21:20
  • @MorganEstes i've also tested with the post meta updates commented out and a duplicate was still created
    – Anagio
    Commented Aug 12, 2013 at 21:24

1 Answer 1

1

You could try to use nonces (http://codex.wordpress.org/WordPress_Nonces) in your requests,so even if the request is being sent many times, only 1 time is being saved.

You can add in your form this line

<form method="POST">

    <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'form-nonce' );?>" />
    ....
</form>

and in the code where you check :

 $nonce = $_POST['nonce'];
 if ( ! wp_verify_nonce( $nonce, 'form-nonce' ) ) {
     die( 'Security check' ); 
 } else {
     //Your code here
 }
3
  • I'll try this now with the nonce mods
    – Anagio
    Commented Aug 12, 2013 at 22:22
  • I removed the nonce from my form and boolean conditions except for the form post in the if condition and used your code seems to be working. Will wait to see if it comes up again by tomorrow and mark your answer. Thanks
    – Anagio
    Commented Aug 12, 2013 at 22:27
  • Seems to be working just fine now, cleared my cache in Safari, submitted the form multiple times with different info and no duplicates. Thanks again
    – Anagio
    Commented Aug 12, 2013 at 23:48

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.