Cannot modify header information during redirect i

2019-07-16 19:41发布

My problem is very similar to the thread Cannot modify header information in wordpress. Unfortunately, it is not marked answered there and cpicko's answer helped me understand the problem but I haven't been able to solve it.

I am trying to redirect a subscriber to home page if he tries to log in his backend profile. I want to do this because I want my site to be totally front-end driven and professional. For this wordpress has example code on http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init. It is as below

function restrict_admin_with_redirect() {
    if ( ! current_user_can( 'manage_options' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    wp_redirect( site_url() ); 
    exit;
    }
}
add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );

When I paste this in my functions.php, it does the redirect but then breaks after header with below error.

"Warning: Cannot modify header information - headers already sent by (output started at /***/wp-content/themes/twentytwelvechild/headerwithquotes.php:15) in /***/wp-includes/pluggable.php on line 1121"

But these files/lines are not modifies by me at all. They are just like fresh wordpress install. The example also is exactly like in wordpress site. This works fine in my dev site but doesn't work when I migrate to production

Cpicko mentioned in that thread that javascript might help. How can I use javascript in this scenario when pages are wordpress default pages and not that build in website. Can someone please help me with this redirection. I would really appreciate it.

2条回答
趁早两清
2楼-- · 2019-07-16 20:35

Sorry, but my English isn't very good. I had the same problem

Warning: Cannot modify header information – headers already sent by
(output started at ./public_html/wp-config.php:1)
in ./wp-includes/pluggable.php on line 1177

After 8 hours i fixed this issue. I just add ; at the end of the first line <?php; of wp-config.php. That's all. Now, I can log-in as wordpress admin.

Hope this helps...

查看更多
走好不送
3楼-- · 2019-07-16 20:42

this could happen due to several reasons. i faced to this issue few days ago when i tried to add a new function to my function.php file. here is the fix i found from the google

wp-config.php issue

1.Open your wp-config.php file with editor to edit it .

2.Remove all the spases before  “<?php ” and after the ” ?>” symbols

3.If  “?>” symbol is missing just add it at the end of the page.

4.Now save your file and check.

functions.php issue

1.Open your functions.php file with editor to edit it  this is located at ” wp-    content/themes/your theme name/functions.php”. 

2.Remove all the space before  “<?php ” and after the ” ?>” symbols If  “?>”

3.symbol is missing just add it at the end of the page. Now save your
file and check.

Source : http://www.mytrickpages.com/2013/11/how-to-fix-wordpress-error-cannot-modify-header-information.html

查看更多
登录 后发表回答