wp_list table get record from database and add view,delete,read and unread items via bulk action

Home » Snippets » wp_list table get record from database and add view,delete,read and unread items via bulk action
0

Created with:

Visibility: 

public

Creator: mahdi

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
    // Custom WP_List_Table class
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');

    // Define your own custom list table class
    class Custom_List_Table extends WP_List_Table {
        // Database table name
        private $table_name;

        public function __construct($table_name) {
            $this->table_name = $table_name;
            parent::__construct(array(
                'singular' => 'item',
                'plural'   => 'items',
                'ajax'     => false
            ));
        }

        // Helper method to get records from the database
        private function get_records() {
            global $wpdb;
            $query = "SELECT * FROM {$this->table_name}";
            return $wpdb->get_results($query, ARRAY_A);
        }

        // Bulk action handler for adding view, delete, read, and unread items
        public function process_bulk_action() {
            // Check if the user performed a bulk action
            if ('bulk-action' === $this->current_action()) {
                $ids = isset($_REQUEST['item']) ? $_REQUEST['item'] : array();

                if (!empty($ids)) {
                    foreach ($ids as $id) {
                        // Perform actions based on action value
                        switch ($_REQUEST['action']) {
                            case 'view':
                                // Add view code here
                                $this->view_item($id);
                                break;
                            case 'delete':
                                // Add delete code here
                                break;
                            case 'read':
                                // Add read code here
                                break;
                            case 'unread':
                                // Add unread code here
                                break;
                        }
                    }
                }
            }
        }

        // View item action handler
        private function view_item($id) {
            // Retrieve desired item details based on $id
            $item = $this->get_item_details($id);
            
            // Perform view action using item details
            if ($item) {
                // Add your view code here
                // Example: Display the item details
                echo "<h2>Item Details:</h2>";
                echo "ID: " . $item['item_id'] . "<br>";
                echo "Name: " . $item['item_name'] . "<br>";
            }
        }

        // Helper method to get item details based on ID
        private function get_item_details($id) {
            global $wpdb;
            $query = $wpdb->prepare("SELECT * FROM {$this->table_name} WHERE item_id = %d", $id);
            return $wpdb->get_row($query, ARRAY_A);
        }

        // Prepare items for display
        public function prepare_items() {
            $this->process_bulk_action();

            $columns  = $this->get_columns();
            $hidden   = array();
            $sortable = $this->get_sortable_columns();

            $this->_column_headers = array($columns, $hidden, $sortable);
            $this->items = $this->get_records();
        }


        // ... rest of the code remains the same ...

    }

    // Example usage
    function display_list_table() {
        $table_name = 'your_table_name'; // Replace with your actual table name
        $list_table = new Custom_List_Table($table_name);
        $list_table->prepare_items();

        echo '<form method="get">';
        $list_table->search_box('Search', 'search');
        $list_table->display();
        echo '</form>';
    }

    // Call the display_list_table function to display the table
    display_list_table();
?>
				

Explanation:

I have added the view_item method to the Custom_List_Table class, which handles the action of viewing an individual item. Inside this method, you can perform any desired action for viewing the item based on the provided item ID. In this example, I have included a simple implementation to display the item details.

Additionally, I have modified the process_bulk_action method to call the view_item method when the "View" action is selected. It retrieves the item details using the get_item_details method, which retrieves specific item details from the database based on the item ID.

Remember to replace 'your_table_name' with the actual name of your table.

With these modifications, you should be able to view individual items by selecting the "View" action from the bulk actions dropdown in the table. Feel free to customize the view_item method with your specific view code.

Register an account to save your snippets or go Pro to get more features.