0

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 .

5
  • Are you running the code (which deletes the data row and do the redirect) within any hook?
    – Sally CJ
    Commented Jun 18, 2019 at 3:00
  • no there is no hook ...
    – Flutterer
    Commented Jun 18, 2019 at 3:04
  • I'd suggest you using hooks like 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:10
  • I am confused in how to use action as you preferred
    – Flutterer
    Commented Jun 18, 2019 at 3:17
  • It's similar to how you use any other hooks like admin_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

2 Answers 2

0

Add 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.

5
  • awesome ... but i dont understand the logic behind it ...
    – Flutterer
    Commented Jun 18, 2019 at 4:54
  • data are printing while theme is rendering output to the browser. meanwhile we redirect to other page using wp_redirect() take away from that page before the whole page is renders to the browser. This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. you can see more about ob_start() at php.net/manual/en/function.ob-start.php Commented Jun 18, 2019 at 5:01
  • okY great thankyou :) one more thing if you can answer ... in the redirection thing i want to print the message after redirecting how can i do that ?
    – Flutterer
    Commented Jun 18, 2019 at 5:02
  • you need to pass query_string variable with info about deleted item then you can use $_GET[''] to get that and dislpay message using add_action( 'admin_notices', 'admin_notices_callback' ); function. Commented Jun 18, 2019 at 5:05
  • I have used sessions for the work but i will surely look into your way . thanks :)
    – Flutterer
    Commented Jun 18, 2019 at 5:15
0

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' );
3
  • I have tried exit; but it is not solving the issue
    – Flutterer
    Commented Jun 18, 2019 at 3:03
  • I've updated my answer with 2 other issues Commented Jun 18, 2019 at 3:06
  • no luck i have tried both of your updated edits .
    – Flutterer
    Commented Jun 18, 2019 at 3:12

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.