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="+ купон нэмэх" />
wp_insert_post
to create a new post. Can you please post that part of the code? Thanks.