首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我一直使用WordPress中的会话重定向到错误的帖子?

为什么我一直使用WordPress中的会话重定向到错误的帖子?
EN

Stack Overflow用户
提问于 2021-08-15 02:01:55
回答 2查看 55关注 0票数 0

我正在制作一个WordPress插件,允许用户在前端创建帖子,然后自动将他们重定向到他们刚刚创建的帖子。我必须使用两个单独的函数和一个会话变量来实现这一点,因为在发送报头之后不能使用wp_redirect。我遇到的问题是会话变量没有发送刚刚创建的帖子的永久链接,而是发送了前一篇帖子的永久链接。

下面的函数在用户提交表单时初始化

代码语言:javascript
复制
function create_post(){
    
if(is_user_logged_in())
{
    if(isset($_POST['ispost']))
    {
     
        global $current_user;
        get_currentuserinfo();

        $user_login = $current_user->user_login;
        $user_email = $current_user->user_email;
        $user_firstname = $current_user->user_firstname;
        $user_lastname = $current_user->user_lastname;
        $user_id = $current_user->ID;

        $post_title = $_POST['title'];
        $sample_image = $_FILES['sample_image']['name'];
        $post_content = $_POST['sample_content'];
        $category = $_POST['category'];

        $new_post = array(
            'post_title' => $post_title,
            'post_content' =>$post_content,
            'post_status' => 'publish',
            'post_type' => $post_type,
            'post_category' => $category
        );
    
        $pid = wp_insert_post($new_post);
        add_post_meta($pid, 'meta_key', true);
       $_SESSION['pid'] = get_post_field( 'post_name', $pid );
        if (!function_exists('wp_generate_attachment_metadata'))
        {
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
        if ($_FILES)
        {
            foreach ($_FILES as $file => $array)
            {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK)
                {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $pid );
            }
        }
        if ($attach_id > 0)
        {
            //and if you want to set that image as Post then use:
            update_post_meta($pid, '_thumbnail_id', $attach_id);
            $_SESSION['pid'] = $pid->post_name;
            
        }

        $my_post1 = get_post($attach_id);
        $my_post2 = get_post($pid);
        $my_post = array_merge($my_post1, $my_post2);
        
        
    }
    

}
else
{
    echo "<h2 style='text-align:center;'>User must be login for add post!</h2>";
}
}

此函数用于将用户重定向到刚刚在init上创建的帖子

代码语言:javascript
复制
add_action('init', 'myInit');
function myInit() {
    if (isset($_POST['ispost'])) {
        $errors = myFormValidation();
        if (empty($errors)) {
            
           // echo get_permalink($pid);
            $url = 'https://somethingsomething.com/'. $_SESSION['pid'];
            wp_redirect($url);
            

          
        }
        //Set the errors here
    }
}

如何使$_SESSION‘’pid‘发送当前帖子的固定链接而不是前一个帖子的永久链接?

EN

Stack Overflow用户

发布于 2021-08-15 08:00:26

可能是因为myInit()函数在create_post()函数之前运行,所以重定向到了错误的位置。

wp_redirect

您需要在调用wp_redirect();之后立即添加exit(),否则会出现意外的结果。

重定向用户的步骤

您可以执行以下操作。

  1. 尝试在发送标头之前处理表单,并使用wp_redirect()重定向用户。

我猜这在理论上是可行的,但可能并不理想,因为我假设create_post()函数是由一个admin_post_(action)钩子处理的,该钩子在发送报头之后运行

  1. 在发送邮件头后使用JavaScript重定向用户。

您可以使用JavaScript在处理表单后重定向用户。

代码语言:javascript
复制
echo '<script type="text/javascript">';
echo 'window.location = "' . $url . '"';
echo '</script>';
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68788240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档