我使用以下代码重定向页面,以防止表单重新提交。它可以很好地重定向到主页。
我的网站是基于WordPress的,因此,一旦(WordPress)帖子的表单上传,我希望在表单提交后重定向到上传的相应帖子(表单从前端上传)
因此,我需要在header()函数中放入什么样的代码。
if(isset($_POST[\'submit-coupon\']) ) {
// other codes
header(\'Location: http://coupon.mn\');
}
我认为重要的代码部分是;(我试图省略非必要代码)
<?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="+ купон нэмэх" />