PHP header() not available to use in my plugin

I have a client website with a custom-developed plugin that when data is updated (this is all in Wordpress admin backend) uses a PHP header('Location: ') function to switch to a different URL. I had originally developed in an environment with an old theme that I eventually scrapped for a newer better one, but now PHP throws an error saying:

PHP Warning: Cannot modify header information - headers already sent by (output started at /wp-admin/includes/template.php:2018) in /wp-content/plugins/*my-plugin-name*/save-data.php on line 47

which is where my header() function is in my plugin.

I originally was convinced it was a plugin conflict, but then I realized it was doing with the twenty fifteen theme and then any other theme I tried other than the crappy old one that was the active theme when I developed the plugin.

Topic plugin-development plugins Wordpress

Category Web


You see that error because something is sending output (even space characters) before the header-location command. The error message tells you where the output is coming from. To fix, you'd have to edit that file - not a wise choice to edit core files, because the next update will overwrite your changes.

Assuming your plugin has a need to redirect, then you should use the wp_redirect() function (see here https://developer.wordpress.org/reference/functions/wp_redirect/ ).

Remember to put a exit; after the wp_redirect() to ensure any code following the wp_redirect is not executed - as the function's docs explain.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.