WordPress的重定向问题,头已经发出(Wordpress redirect issue, he

2019-07-31 23:01发布

I am wondering, based on the code bellow, where I would want to put my wp_redirect function because where it currently is does nothing but spazzes out and sais:

 Warning: Cannot modify header information - headers already sent by (output started at /***/***/WordPress/WordPressDev/wp-includes/script-loader.php:664) in /***/***/WordPress/WordPressDev/wp-includes/pluggable.php on line 881

Which I get because the page has already loaded. but I am un sure where to call this function.

I have replace my web site and any "personal data" with stars and example.com. How ever this code does work, it just wont redirect me.

thoughts?

function get_latest_version_zip(){
             global $wp_filesystem;

             if(current_user_can('update_themes')){
                $aisis_file_system_structure = WP_Filesystem();
                $aisis_cred_url = 'admin.php?page=aisis-core-update';
                if($aisis_file_system_structure == false){
                    request_filesystem_credentials($aisis_cred_url);
                    $this->credential_check = true;
                }

                $aisis_temp_file_download = download_url( 'http://example.com/aisis/aisis_update/Aisis2.zip' );

                if(is_wp_error($aisis_temp_file_download)){
                    $error = $aisis_temp_file_download->get_error_code();
                    if($error == 'http_no_url') {
                        add_action( 'admin_notices', 'aisis_framework_download_update_erors' );
                    }
                }

                $aisis_unzip_to = $wp_filesystem->wp_content_dir() . "/themes/" . get_option('template');

                $this->delete_contents_check(); //Check if we need to delete the aisis core folder.

                $aisis_do_unzip = unzip_file($aisis_temp_file_download, $aisis_unzip_to);

                unlink($aisis_temp_file_download); //delete temp jazz

                if(is_wp_error($aisis_do_unzip)){
                    $error = $aisis_do_unzip->get_error_code();
                    if($error == 'incompatible_archive') {
                        $this->aisis_incompatible_archive_errors();
                    }
                    if($error == 'empty_archive') {
                        $this->aisis_empty_archive_errors();
                    }
                    if($error == 'mkdir_failed') {
                        $this->aisis_mkdir_failed_errors();
                    }
                    if($error == 'copy_failed') {
                        $this->aisis_copy_failed_errors();
                    }
                    return;
                }
                //throwing errors
                wp_redirect(admin_url('admin.php?page=aisis-core-options'));
                exit;

             }
         }

in my functions.php file I placed the following code:

 function callback($buffer){
     return $buffer;
 }

 function add_ob_start(){
     ob_start("callback");
 }

 function flush_ob_end(){
     ob_end_flush();
 }

 add_action('wp_head', 'add_ob_start');
 add_action('wp_footer', 'flush_ob_end');

with this I still get the error, I think I misunderstanding something....

Answer 1:

刚刚替换下面的行

add_action('wp_head', 'add_ob_start');

add_action('init', 'add_ob_start');

输出缓冲发送任何事情之前应该开始/回显到浏览器和wp_head钩后来发生略高于init钩到那时头已经发出,并保持/将其放置在你的顶部functions.php任何事情之前呼应/发送到浏览器。



Answer 2:

问题是,地方在WordPress中header()函数被调用和一些输出已经发送到客户端,而output buffering处于关闭状态。

头有任何输出之前发送,否则,你得到你所描述的错误。

wp_redirect(admin_url('admin.php?page=aisis-core-options'));

上述线设置像这样的标题: header('Location: admin.php......');

通过php.ini中打开输出缓冲,在Wordpress或简单的事情之前的index.php文件被echo'ed给客户应采取错误的照顾。

详细信息/文件可以在这里找到: http://php.net/manual/en/book.outcontrol.php

我能想到的最简单的方法就是让你的WordPress的index.php是这样的:

ob_start();
// content of your index.php here
ob_flush();


Answer 3:

另一种可能性是增加一个priotity:

add_action('wp_head', 'add_ob_start', 1);

第三个参数是$优先级。

另外,如果你在一个以上功能挂钩,它可以让你执行链的完全控制。



文章来源: Wordpress redirect issue, headers already sent