Wordpress redirect issue, headers already sent

江枫思渺然 提交于 2019-12-20 05:14:28

问题


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....


回答1:


Just replace the following line

add_action('wp_head', 'add_ob_start');

with

add_action('init', 'add_ob_start');

Output buffering should start before anything sent/echoed to the browser and wp_head hook occurs a bit later than init hook and till then headers already sent and also Keep/place it at the top of your functions.php before anything echoed/sent to the browser.




回答2:


The problem is that somewhere in wordpress the header() function has been called and some output was already sent to the client while output buffering is off.

Headers have to be sent before any output, otherwise you get the error you described.

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

The above line sets a header like this: header('Location: admin.php......');

Turning on output buffering via php.ini, at the index.php of wordpress or simply before anything is echo'ed to the client should take care of the error.

Details/Documentation can be found here: http://php.net/manual/en/book.outcontrol.php

simplest way i can think of is make your wordpress index.php look like this:

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



回答3:


Another possibility would be adding a priotity:

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

The third param is $priority.

Also, if you're hooking in more than one function, it gives you complete control of the execution chain.



来源:https://stackoverflow.com/questions/12608881/wordpress-redirect-issue-headers-already-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!