I have created a table in which i have delete link something like http://localhost/wptheme/wp-admin/admin.php?page=batch-op-settings&action=batch-delete&post_id=5 and i have added a code to delete the post
if ($action == 'batch-delete') {
require_once (plugin_dir_path( __FILE__ ).'views/single_batch_delete.php');
}
code in the required file is
if (isset($_GET['action']) == 'batch-delete') {
global $wpdb;
$delete_batch = $wpdb->delete( 'batch_number', array(
'id'=>$_GET['post_id']
) );
wp_redirect( $_SERVER['PHP_SELF'].'?page="batch-op-settings"', 302, 'Deleted' );
}
the data row is deleted but it is not redirecting to the all batches code all data i have used wp_redirect function i have also used header() function but it shows this error
Warning: Cannot modify header information - headers already sent
I want to know is there any better way to handle this issue or any solution with the current code .
I have created a table in which i have delete link something like http://localhost/wptheme/wp-admin/admin.php?page=batch-op-settings&action=batch-delete&post_id=5 and i have added a code to delete the post
if ($action == 'batch-delete') {
require_once (plugin_dir_path( __FILE__ ).'views/single_batch_delete.php');
}
code in the required file is
if (isset($_GET['action']) == 'batch-delete') {
global $wpdb;
$delete_batch = $wpdb->delete( 'batch_number', array(
'id'=>$_GET['post_id']
) );
wp_redirect( $_SERVER['PHP_SELF'].'?page="batch-op-settings"', 302, 'Deleted' );
}
the data row is deleted but it is not redirecting to the all batches code all data i have used wp_redirect function i have also used header() function but it shows this error
Warning: Cannot modify header information - headers already sent
I want to know is there any better way to handle this issue or any solution with the current code .
Share Improve this question edited Jun 18, 2019 at 14:03 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jun 18, 2019 at 2:32 FluttererFlutterer 1159 bronze badges 5 |2 Answers
Reset to default 0Add below code in functions.php and check it works or not.
add_action('admin_init', 'app_output_buffer');
function app_output_buffer() {
ob_start();
}
let me know if it is working or not.
The issue is likely your condition:
if (isset($_GET['action']) == 'batch-delete') {
isset()
returns true
or false
, not the value of the parameter. If you want to also check the value you need to check it like this:
if ( isset( $_GET['action'] ) && 'batch-delete' === $_GET['action'] ) {
Then make sure you use exit;
after running wp_redirect()
. From the documentation:
Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to
exit;
:wp_redirect( $url ); exit;
Exiting can also be selectively manipulated by using wp_redirect() as a conditional in conjunction with the ‘wp_redirect’ and ‘wp_redirect_location’ filters:
if ( wp_redirect( $url ) ) { exit; }
Lastly, for the redirect URL, you'd be better off using the admin_url()
function. Plus, the page
query string shouldn't be in quotes.
wp_redirect( admin_url( 'admin.php?page=batch-op-settings' ), 302, 'Deleted' );
admin_action_batch-delete
to handle your plugin action. From hooks like that, performing redirects shouldn't give you the "headers already sent" issue. – Sally CJ Commented Jun 18, 2019 at 3:10admin_menu
-add_action( 'admin_action_batch-delete', function(){ // update/delete your data and then redirect } )
. Example – Sally CJ Commented Jun 18, 2019 at 4:19