在Wordpress 3.6.1中“警告:无法修改标头信息”

时间:2013-10-15 10:09:21

标签: php wordpress warnings

我尝试在Wordpress3.6.1的测试插件中使用wp_redirect,这是一个简单的插件,但它不起作用。这是我的代码:

<?php
/*
Plugin Name: Test
Plugin URI: http://localhost
Description: This is just test plugin
Author: author
Version: 1.0
Author URI: http://localhost
*/
function test_menu(){
    $page_title = 'Test';
    $menu_title = 'Test';
    $capability = 'manage_options';
    $menu_slug = 'test-menu';
    $function = 'my_test';
    add_menu_page($page_title, $menu_title, $capability,$menu_slug, $function);
}

add_action('admin_menu', 'test_menu');

function my_test(){
    wp_redirect("http://www.google.com.vn");
    exit;
}

单击“测试”菜单时,我收到警告:

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\wp3.6.1\wp-admin\includes\template.php:1706) in C:\AppServ\www\wp3.6.1\wp-includes\pluggable.php on line 875

请帮助我,这段代码会发生什么

2 个答案:

答案 0 :(得分:3)

尝试使用ob_start()

进行缓冲
add_action('init', 'do_output_buffer');
function do_output_buffer() {
        ob_start();
}

答案 1 :(得分:0)

它完全在我的工作中,可能存在可变价值冲突。

我用过这个。试试这个。

<?php
/*
Plugin Name: Test
Plugin URI: http://localhost
Description: This is just test plugin
Author: author
Version: 1.0
Author URI: http://localhost
*/
function test_menu(){
    $page_title = 'Test';
    $menu_title = 'Test';
    $capability = 'manage_options';
    $menu_slug = 'test-menu';
    $function = 'my_test';
    add_menu_page($page_title, $menu_title, $capability,$menu_slug, $function);
}

add_action('admin_menu', 'test_menu');

function my_test(){
    wp_redirect("http://www.google.com.vn");
    exit;
}
?>

由于