Skip to content

Instantly share code, notes, and snippets.

@5ally

5ally/README.md Secret

Last active June 18, 2019 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5ally/c4892a9ce9bf20799fd3040f8c7af080 to your computer and use it in GitHub Desktop.
Save 5ally/c4892a9ce9bf20799fd3040f8c7af080 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Test Plugin
* Version: 20190618.1
* Text Domain: test-plugin
*/
defined( 'ABSPATH' ) || exit;
add_action( 'admin_menu', 'register_admin_menu' );
function register_admin_menu() {
add_menu_page( 'Test Plugin', 'Test Plugin', 'manage_options', 'batch-op-settings', 'render_admin_page' );
}
function render_admin_page() {
?>
<div class="wrap">
<h1>Test Plugin</h1>
<?php if ( ! empty( $_GET['batch_deleted'] ) ) : ?>
<div class="notice notice-info is-dismissible">
<p>Dummy batch deletion was successful!</p>
</div>
<?php endif; ?>
<p><a href="<?php echo esc_url( add_query_arg( [
'action' => 'batch-delete',
'post_id' => 5,
// Don't add the "page" parameter.
], admin_url( 'admin.php' ) ) ); ?>" class="button">Delete</a> (<i>nothing will be deleted</i>)</p>
</div>
<?php
}
add_action( 'admin_action_batch-delete', 'batch_delete_data' );
function batch_delete_data() {
// Update/delete your data.
error_log( 'Yay, it works!' );
// Back to the plugin page.
$menu_page_url = menu_page_url( 'batch-op-settings', false );
wp_redirect( add_query_arg( 'batch_deleted', '1', $menu_page_url ) );
exit;
}
@samairali
Copy link

thankyou sally for doing so much effort for me :)

@samairali
Copy link

I am trying to understand the code and i have understood something and i think that wp_redirect function will not work either because due to wp_redirect the issue is coming header is already set .

@5ally
Copy link
Author

5ally commented Jun 18, 2019

@samairali In that case, try posting the full code as a gist (don't post here). And update your WPSE question so that others can check it, too.

But I'm guessing you know what could be the reasons to the issue ("headers already sent"), so you should make sure there has been no output printed before you call wp_redirect().

A simple space in your functions file could cause the issue, you know. So try to find the code which may be printing the output and fix that code.

@samairali
Copy link

@5ally issue is resolved now ... i want to know something if you have free time ?

@samairali
Copy link

yes exactly that was the issue , i was not printing but might be wordpress core files were doing something but i have used ob_start at the start and it is ok now ... it is redirected now but how can i print message on redirection that specific thing is deleted ?

@5ally
Copy link
Author

5ally commented Jun 18, 2019

how can i print message on redirection that specific thing is deleted

Look at the sample plugins again. You could see I printed a sample notice there.

You could also use the admin_notices hook or add_settings_error().

@5ally
Copy link
Author

5ally commented Jun 18, 2019

i have used ob_start at the start and it is ok now

Ok, but you should try to disable all plugins except yours and try a default theme as well. Doing that could help you with a proper fix than a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment