[wp-trac] [WordPress Trac] #62430: Bulk Action: Cannot submit bulk action if no items are selected

WordPress Trac noreply at wordpress.org
Mon Nov 18 00:40:39 UTC 2024


#62430: Bulk Action: Cannot submit bulk action if no items are selected
----------------------------+------------------------
 Reporter:  jcutler         |       Owner:  joedolson
     Type:  defect (bug)    |      Status:  accepted
 Priority:  normal          |   Milestone:  6.7.1
Component:  Administration  |     Version:  6.7
 Severity:  blocker         |  Resolution:
 Keywords:                  |     Focuses:
----------------------------+------------------------

Comment (by jcutler):

 Here's a fairly straight-forward example, excluding most of the required
 WP_List_Table implementation.

 {{{#!php
 <?php
 class My_List_Table extends WP_List_Table {
     ...

     public function get_bulk_actions() {
         $actions = [
             'download-selected' => 'Download Selected',
             'download-all' => 'Download All',
         ];
         return $actions;
     }
     ...
     public function get_ui() {
 ?>
 <div class="wrap">
     <h1 class="wp-heading-inline">Registrations</h1>
     <hr class="wp-header-end">
     <form id="registrations" method="post">
         <?php echo $this->views(); ?>
         <?php wp_nonce_field('manage_registrations',
 'registration_nonce'); ?>
         <div id="post-body-content">
             <div class="table">
                 <?php
                 $this->prepare_items();
                 $this->display();
                  ?>
             </div>
         </div>
     </form>
 </div>
 }
 ?>
 }}}

 {{{#!javascript
 <script>
 document.addEventListener('DOMContentLoaded', function () {
     const form = document.querySelector('form');
     const bulk_actions_input = form.querySelector('input[name="bulk-
 actions"]');

     form.addEventListener('submit', function (event) {
         bulk_actions_submit(event);
     });
 });

 function bulk_actions_submit(event) {
     var action = $('#bulk-action-selector-top').val();
     var selected_item_count = $('input[name="bulk-
 action[]"]:checked').length;
     if( "-1" === action ){
         alert('Please select an action.');
         event.preventDefault();
         return false;
     }
     if(selected_item_count <= 0 && action !== 'download-all'){
         alert('Please select one or more items.');
         event.preventDefault();
         return false;
     }
     //allow form submit
 }
 </script>
 }}}

 Much of the above code is ChatGPT generated rather than try to simplify my
 plug-in code.

 I would expect there are other developers that have implemented
 functionality similar to this in their plug-ins. Mine is a custom client
 plug-in that I can't share properly, but this should give you the idea of
 the workflow.

 As a note on the importance of this, if I had not been actively working on
 a new feature and simply upgraded to version 6.7 I would not have known
 6.7 broke my implementation until the customer reported this; client-side
 functionality is generally not a test I would run for a WordPress update.
 I have six different pages with this behavior, and having to modify and
 test those areas with what I consider essentially a breaking API change is
 not ideal. I would hope you will take into consideration how this may
 impact other developers as well. The only reasonable solution in my
 opinion would be to make the new functionality configurable, with the
 default behavior being disabled. I probably would have been pleased if
 this were part of the list table functionality when I first began working
 with this.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62430#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list