最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp list table - I want to redirect the url to the previous page

programmeradmin1浏览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 .

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
  • 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
Add a comment  | 

2 Answers 2

Reset to default 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.

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' );
发布评论

评论列表(0)

  1. 暂无评论