0

My code is like below.

$result = $wpdb->query($sql);
if ( $result ) {
  wp_redirect( "http://www.example.com/contact-us");
  exit();
}

I am getting below error

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wordpressplugin/wp-admin/includes/misc.php:1281) in /var/www/html/wordpressplugin/wp-includes/pluggable.php on line 1296

I tried with below code but I didn't get any solution

$result = $wpdb->query($sql);
if ( $result ) {
  ob_start();
  wp_redirect( "http://www.example.com/contact-us");
  ob_end_flush();
  exit();
}
1
  • 3
    Where are you putting this code? In a hook? In a file? "Headers already sent" means you're trying to redirect too late in the page load process.
    – Howdy_McGee
    Commented Aug 24, 2020 at 16:38

1 Answer 1

-1

Add ob_start(); at the top of functions.php file and remove from your code. As WP is a combination of plugin and core files and we don't know which code sends the throws the header sent warning. And we know that functions.php is called every time we made a request to server, so this is the ideal place to add the ob_start() code to overcome the problem.

3
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Aug 24, 2020 at 22:04
  • Thanks @Pradipta Sarkar. I am developing a plugin. Where should I place ob_start(); ? Thanks
    – abu abu
    Commented Aug 25, 2020 at 11:26
  • Can you add to your main plugin file and do a test? Commented Aug 25, 2020 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.