0

I'm using the following code to redirect the page, in order to prevent re-submission of the form. And it does fine job doing redirection to the homepage.

And my website is based on the WordPress, therefore once the (WordPress) post's form is uploaded, I want redirection to the uploaded respective post after form submission (form is uploaded from the front-end)

Therefore what kind of code, do I need to put in the header() function.

if(isset($_POST['submit-coupon']) ) {
    // other codes
     header('Location: http://coupon.mn');     
}

I think the essential code part is; (I try to omitted non-essential code)

<?php   
if(isset($_POST['submit-coupon']) ) {
    require get_template_directory() . '/includes/mediaUpload.php';

    // error handling codes

    if($_FILES['coupon_image']['error'] != 0) {
        $errors[] = __('Купон-ийн зураг оруулна уу', 'Teo');
    }
    if(count($errors) == 0) {
        $allowed_tags = wp_kses_allowed_html( 'post' );
        $title = esc_attr($_POST['coupon_title']);
        $description = wp_kses($_POST['coupon_description'], $allowed_tags);
        if(is_user_logged_in() ) { 
            $current_user = wp_get_current_user();
            $user_id = $current_user->ID;
        }
        else {
            $user_id = 1;
        }
        $post = array(
        'comment_status' => 'close', /* added by AB, open */
        'ping_status'    => 'close', /* added by AB, open */
        'post_author'    => $user_id,
        'post_content'   => $description,
        'post_status'    => 'publish', /* added by AB, pending */
        'post_title'     => $title,
        'post_type'      => 'coupons',
        );  
        $post_id = wp_insert_post($post);
        if($post_id == 0) {
            $errors[] = __("The listing couldn't be added to the database, try again.", "Teo");
        }
        else {
            $cat = (array)$cat;
            wp_set_post_terms($post_id, $cat, 'category'); //setting the correct category
            $tmp = new MediaUpload;
            if($_FILES['coupon_image']['error'] == 0) {
                $featured_image = $tmp->saveUpload( 'coupon_image' );
                add_post_meta($post_id, '_thumbnail_id', $featured_image['attachment_id']);
            }
            //setting custom fields info
            if($discount != '') {
                add_post_meta($post_id, '_single_discount', $discount);
            $ok=1;
        }
    }
    header('Location: http://coupon.mn');     
}?>


<div class="text-content frontend-submit">
<?php if(isset($errors) && count($errors) > 0) { ?>
    <div class="large-12 column alert alert-danger">
        <?php foreach($errors as $error) echo $error . '<br />'; ?>
    </div>
<?php } ?>
<?php if(isset($ok) ) { ?>
    <div class="large-12 column alert alert-success">
        <?php _e('Your post is added!', 'Couponize');?>
    </div>
<?php } ?>
<form enctype="multipart/form-data" action="<?php echo(get_permalink($post->ID)); ?>" method="post">


    // MORE INPUT FORM

    <!-- SUBMIT BUTTON -->
    <div class="cf"></div> <br />
    <div class="large-12 column">
        <input class="input button primary red, large-3" type="submit" name="submit-coupon" value="+ купон нэмэх" />
4
  • 1
    Not sure I understand what is actually the issue. In addition what makes it a wordpress specific issue and not a general PHP one? Commented Jun 9, 2016 at 16:25
  • @MarkKaplun - Sorry for my incomplete explanation, I updated the question. Thanx Commented Jun 9, 2016 at 16:43
  • 1
    @anand_mongol I suppose you are using wp_insert_post to create a new post. Can you please post that part of the code? Thanks. Commented Jun 9, 2016 at 17:00
  • @BorisKuzmanov - I uploaded the more code, which I think essential. Yes I do use the 'wp_insert_post'. Thank you Commented Jun 9, 2016 at 17:19

1 Answer 1

4

Since you are already getting the ID of the newly created post ($post_id), you just need to get the URL and redirect after the submission.

Using get_permalink( $post_id ) you can get post's URL. Also, you can get rid of the header() function to send a new HTTP header, and use WordPress' own function for redirect - wp_redirect().

wp_redirect( get_permalink( $post_id ) );
die();

https://developer.wordpress.org/reference/functions/wp_insert_post/ https://developer.wordpress.org/reference/functions/get_permalink/ https://developer.wordpress.org/reference/functions/wp_redirect/

3
  • It works like a Charm!!! - THANKS, can I ask you one more questions - is it possible to show the post in the respective category listing - in this case it's in the category (fastfood) - therefore I want to redirect to respective category listing. Commented Jun 9, 2016 at 17:42
  • You can use the same approach, but instead of post's, you will be using category's url. You may find get_category_link() and get_category_by_slug() helpful in that scenario. Commented Jun 9, 2016 at 17:57
  • I try to use the same approach, it is giving me blank page, any suggestion? Commented Jun 9, 2016 at 18:24

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.