wordpressplugins

Show info message in plugin settings page when deleting rows from table in database wordpress


1. HTML part in callback-function in plugin settings page: I am deleting rows in database table like so:

<form action="<?php echo admin_url('admin-post.php'); ?>" method="POST">
   <?php wp_nonce_field( 'my_delete_event'); ?>
   <table class="form-table">
      <tr valign="top">
         <span class="description">Press Button to delete rows.</span>
      </tr>
   </table>
      <input type="hidden" name="action" value="my_delete_event">
      <input type="submit" name="delete_rows" class="button button-primary" value="Delete Rows in Database">
</form>

2. PHP part in plugin settings page: Then via admin_post_{$action} i trigger deletion:

add_action( 'admin_post_my_delete_event', function () {
   global $wpdb;
   $table  = $wpdb->prefix . 'toa_tracking';

   if (!empty($_POST['delete_rows'])) {
      check_admin_referer( 'my_delete_event');
      // delete all rows
      $delete = $wpdb->query("TRUNCATE TABLE $table");
      }

      wp_redirect(admin_url('/admin.php?page=toa_settings&tab=tab1'));
      exit;
 });

I’ve tried many different approaches. Here is one of many failures:

add_action( 'admin_notices', 'toa_notice' );
    function toa_notice() {
    global $wpdb;
    $table  = $wpdb->prefix . 'toa_tracking';
    $num_rows = $wpdb->get_var("SELECT COUNT(*) FROM $table");
    if ($num_rows === 0){
        add_settings_error(
        'toa_delete_info',
        'delete',
        'All rows deleted',
        'info' // success, warning, info
        );
        settings_errors( 'toa_delete_info' );
     }
}

But finally i can’t help posting this question, for i don’t know how to trigger an admin notice with this technique. I know how it works with option fields, but in this context (admin_post_{$action}), i have no idea how to trigger a info message. Thanks for your feedback.


Solution

  • I have done similar stuff. I set a transient indicating the database operation has happened right after I do it. Then, when rendering the redirect target page I check the transient. If it’s set I show the notice and clear the transient.

    My exact code is too gnarly to post here and have it make sense. Plus, I'm not looking for laughs, and I would get some.

    Here's what you do, basically. (not debugged.)

    Right after the TRUNCATE TABLE operation do something like this:

    set_transient( 'toa-tracking-table-truncated', true, HOUR_IN_SECONDS );
    
    

    Then, in your 'admin_notices' hook do something like this.

    add_action( 'admin_notices', 'toa_notice' );
        function toa_notice() {
        $truncated = get_transient( 'toa-tracking-table-truncated' );
        if ( $truncated ) {
          delete_transient( 'toa-tracking-table-truncated' );
          ?>
            <div class="notice notice-info toa is-dismissible">
              <p>Table truncated! Hope you meant it, cause your data is gone forever.</p>
            </div>
          <?php
        }
    }
    

    So, your delete-event action uses a transient as a flag saying, "hey next time we render the admin notices for that panel, show a notice." Then the admin notices action does that, and clears the flag.

    This isn't quite perfect, because if two people are looking at the panel concurrently, only one will get the notice. You could use an entry in the wp_usermeta table instead. I used a transient because it expires and you don't have to clean it up in your deactivation hook.