【问题标题】:wp_redirect not workingwp_redirect 不工作
【发布时间】:2013-01-12 09:49:31
【问题描述】:

我正在尝试发布到页面并在帖子中插入一些 wordpress,但不知何故重定向不起作用,之后。我尝试了以下三个选项:

  1. wp_redirect($location);
  2. wp_safe_redirect($location);
  3. header('Location:http://example.com');

这是我想做的:

  1. 发布到另一个页面。
  2. 查找要上传的文件和上传目录
  3. 上传文件。
  4. 使用 set_post_thumbnail 设置帖子缩略图
  5. 重定向到所需页面。

脚本一直工作到设置缩略图然后死掉。 如果有人想用它从模板前端上传缩略图,他们可以使用它。


// if you have this in a file you will need to load "wp-load.php" to get access to WP functions. If you post to "self" with this code then WordPress is by default loaded
    require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
    // require two files that are included in the wp-admin but not on the front end. These give you access to some special functions below.
    require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
    require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php";

    // required for wp_handle_upload() to upload the file
    $upload_overrides = array( 'test_form' => FALSE );

    global $current_user;
    get_currentuserinfo();
    $logged_in_user = $current_user->ID;

    //get user POST information
    global $post;
    $loop = new WP_Query( array( 
                            'posts_per_page' => 1, 
                            'post_type' => "vet-profile", 
                            'order' => "ASC", 
                            'orderby' => "menu_order", 
                            'author'=>"$logged_in_user"
                        )
    );
    while ( $loop->have_posts() ): $loop->the_post();
        $current_postID = $post->ID;
        $current_posttitle = get_the_title(); 
    endwhile;   

    // count how many files were uploaded
    $upload_files = $_FILES[ 'upload_files' ];

    // load up a variable with the upload direcotry
    $uploads = wp_upload_dir();

    // foreach file uploaded do the upload
    //foreach ( range( 0, $count_files ) as $i ) 
    //{
        // create an array of the $_FILES for each file
        $file_array = array(
            'name' => $_FILES['upload_files']['name'],
            'type'  => $_FILES['upload_files']['type'],
            'tmp_name'  => $_FILES['upload_files']['tmp_name'],
            'error' => $_FILES['upload_files']['error'],
            'size'  => $_FILES['upload_files']['size'],
        );

        // check to see if the file name is not empty
        if ( !empty( $file_array['name'] ) ) 
        { ?>
     <?php
            // upload the file to the server
            $uploaded_file = wp_handle_upload( $upload_files, $upload_overrides );

            if ( $uploaded_file ) 
            {
                echo '<script>alert("Image successfully uploaded.\n");</script>';
            } 
            else 
            {
                echo '<script>alert("Fish! some error occured. Please try again.");</script>';
            } 

            // checks the file type and stores in in a variable
            $wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );   

            // set up the array of arguments for "wp_insert_post();"
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
                'post_content' => '',    
                'post_author' => $logged_in_user,
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_parent' => $current_postID,
                'guid' => $uploads['url'] . '/' . $file_array['name']
            );


            // insert the attachment post type and get the ID
            $attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $current_postID );

            // generate the attachment metadata
            $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );

            // update the attachment metadata
            wp_update_attachment_metadata( $attachment_id, $attach_data);

            // set thumbnail to the current post
            set_post_thumbnail( $current_postID, $attachment_id );

            echo '<script>alert("set thumbnail")';
            $location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
            wp_safe_redirect( $location ); 
            echo '<script>alert("End Loop");</script>';
        }

提前致谢

【问题讨论】:

    标签: php redirect wordpress-theming wordpress


    【解决方案1】:

    我发现我可以可靠地调用wp_redirect 的唯一位置是在init 操作挂钩(http://codex.wordpress.org/Plugin_API/Action_Reference/init)的回调函数中,正如文档中明确指出的那样:

    在 WordPress 完成加载之后但在任何标题之前运行 发送。对于拦截 $_GET 或 $_POST 触发器很有用。

    您可能想对此进行调查。如果这对您不起作用,您可能想尝试的另一件事是使用输出缓冲。这是一个关于如何做到这一点的简洁教程:

    http://tommcfarlin.com/wp_redirect-headers-already-sent/

    【讨论】:

      【解决方案2】:
          echo '<script>alert("set thumbnail")';
          $location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
          wp_safe_redirect( $location ); 
          echo '<script>alert("End Loop");</script>';
      

      上面的代码肯定行不通。将某些内容写入输出后,您将无法重定向。我没有检查其余的代码,但如果它按你说的那样工作,删除顶行和底行(或者甚至只是顶行)应该可以使它全部工作:

      $location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
      wp_safe_redirect( $location );
      

      (如果您在 wp-config.php 中启用调试模式,您应该会看到一条错误消息,而不仅仅是白屏。因此,如果这不起作用,您可以将错误复制到这里,我们可以确切地知道是什么问题是。)

      【讨论】:

      • 谢谢卡勒。我怎么能忘记这一点。它现在就像一个魅力。再次感谢您的帮助。
      • @DeepakVerma 如果答案正确,则将其标记为已接受。不这样做被认为是忘恩负义。
      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 2015-09-30
      • 2016-10-21
      • 2018-04-16
      • 1970-01-01
      相关资源
      最近更新 更多