In the development of WordPress backend program, sometimes encounter headers already sent problem, many times because we sent the HTTP header in PHP and then use the wp_redirect function to jump the page, and the wp_redirect function is also through the HTTP Header sent to the The wp_redirect function also does this by sending an HTTP header to the browser.
Reasons for headers already sent problem
We know that in the same http request, only one http header can be sent to the browser, so with the HTTP header has been sent in front of the wp_redirect function and then send HTTP Header request browser is certainly not received. In the HTTP protocol, the request sent out is like splashing water, can not be modified, in order for us to find this problem in time, and then solve this problem, PHP runtime will warn us that there is a problem here, let us check.
And this problem is not a fatal error, it will only cause part of the program to be invalid, so even if we don't solve it and just turn off the warning display, it's okay, but as a programmer in charge, even if it's a warning level error, we have to give him a solution to make sure that the program can be executed accurately.
How to solve header already sent problem
In order to use page jumps, we can only send header requests to the browser in advance. Let's look at a piece of code, the following code is mounted on the admin_init hook, in the WordPress management interface initialization after the execution of the main role of the code is to determine the current page and the type of operation, if the type of operation meets certain conditions, the implementation of some data processing and jump operations.
add_action('admin_init', function ()
{
if (isset($_GET[ 'page' ]) && $_GET[ 'page' ] === 'list_serial' && isset($_GET[ 'action' ]) && $_GET[ 'action' ] === 'delete') {
$ids = isset($_GET[ 'serial' ]) ? (array)$_GET[ 'serial' ] : [];
$sendback = remove_query_arg(['trashed', 'untrashed', 'deleted', 'locked', 'ids'], wp_get_referer());
wp_redirect(add_query_arg(['trashed' => count($ids), 'ids' => join('_', (array)$ids), 'locked' => 1], $sendback));
exit;
}
});
This code is extracted from a page created using the WP_List_Table class. It is an operation that deletes some data from a list, then jumps back to the data list page and displays that the deletion was successful. when WP_List_Table is executed, the page is already initialized, and when the data deletion method of WP_List_Table is executed, the HTTP header must have already been sent. When we execute the data deletion method in WP_List_Table, the HTTP header has already been sent, so if we implement the jump after deletion operation in this class, we will encounter a warning that the header has already been sent. So we need to jump out of this class and determine the page operation and whether we need to jump before the page is initialized.
The above code is in the WordPress administration background operations, in the WordPress front, will also encounter such problems, you can refer to the above code to solve the problem, replace the hook with the initialization of the foreground page hook can be.