“Warning: Cannot modify header information” in Wor

2019-06-14 19:09发布

This question already has an answer here:

I tried to use wp_redirect in my test plugin in Wordpress3.6.1, this is a simple plugin but it did not work. This is my code:

<?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;
}

When click Test menu, i got a warning:

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

Please help me, what's happen with this code

2条回答
戒情不戒烟
2楼-- · 2019-06-14 19:45

Try buffering with ob_start()

add_action('init', 'do_output_buffer');
function do_output_buffer() {
        ob_start();
}
查看更多
我只想做你的唯一
3楼-- · 2019-06-14 19:46

it is completely working in mine., possibly there might be variable value conflicts.

i used this one. try this.

<?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;
}
?>

Thanks

查看更多
登录 后发表回答