重定向至从前端提交表单后上传(WordPress)帖子

时间:2016-06-09 作者:anandmongol

我使用以下代码重定向页面,以防止表单重新提交。它可以很好地重定向到主页。

我的网站是基于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="+ купон нэмэх" />

1 个回复
SO网友:Boris Kuzmanov

因为您已经获得了新创建帖子的ID($post_id), 您只需在提交后获取URL并重定向即可。

使用get_permalink( $post_id ) 您可以获取帖子的URL。此外,您还可以摆脱header() 函数发送新的HTTP标头,并使用WordPress自己的函数进行重定向-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/

相关推荐

TEMPLATE_REDIRECT挂钩重定向错误的URL

我制作了关于登录、注册和忘记密码的自定义插件。我给我的插件添加操作template_redirect 钩子来验证电子邮件,当用户登录时,然后重定向到主页,并且忘记了密码。但我有错误,我认为template\\u重定向挂钩不知道哪个是用于验证的重定向,哪个是用于忘记密码的重定向。请查看我的代码:这是自定义插件。php <?php if( ! defined(\'PLUGIN_PATH\') ){ define(\'PLUGIN_PATH\