WP 6.5 WP_List_Table some issue with Screen Option
-
I have created a WP plugin using WP_List_Table I have issue with screen option
it can display screen option with columns check box, it trigger check and un-check hide/show column but when save it, in screen option is saved, but in table not hide/show column same as screen option.function toty_emp_display_admin_menu() { $toty__hook = add_menu_page('EMS', 'EMS', 'manage_options', 'emp-list', 'da_ems_list_callback'); // screen option add_action("load-$toty__hook", 'my_emp_tbl_add_options'); function my_emp_tbl_add_options() { $option = 'per_page'; $args = array( 'label' => 'Number of items per page:', 'default' => 20, 'option' => 'employees_per_page' ); add_screen_option($option, $args); $empTable = new Employees_List_Table(); }; } // End toty_emp_display_admin_menu add_action('admin_menu', 'toty_emp_display_admin_menu'); // get saved screen meta value add_filter('set-screen-option', 'my_emp_table_set_option', 10, 3); function my_emp_table_set_option($status, $option, $value) { var_dump($value); //exit; return $value; }
- This topic was modified 7 months ago by Ty Chhoun.
- This topic was modified 7 months ago by Ty Chhoun.
- This topic was modified 7 months ago by Ty Chhoun.
- This topic was modified 7 months ago by Ty Chhoun.
- This topic was modified 7 months ago by Ty Chhoun.
- This topic was modified 7 months ago by Ty Chhoun.
-
If I understand correctly, you want to make the columns in your table activatable and deactivatable? You don’t have to manually extend the screen options for this. All you need to do is add the columns to your table class. Could you please show the source code so that we can understand the whole thing?
Thanks you so much, here is my hold code & May video https://www.youtube.com/watch?v=4GoGhH3RZts <?php /* * Plugin Name: Custom TOTY * Description: My plugin to explain the crud functionality. * Version: 1.0 * Author: ToTy Monthana * Plugin URI: https://estccomputer.com/ * Author URI: https://estccomputer.com/ */ define("TOTY_PLUGIN_DIR_PATH",plugin_dir_path(__FILE__)); define("TOTY_PLUGIN_URL", plugins_url()); register_activation_hook(__FILE__, 'table_creator'); function table_creator() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $table_name = $wpdb->prefix . 'ems'; $sql = "DROP TABLE IF EXISTS $table_name; CREATE TABLE $table_name( id mediumint(11) NOT NULL AUTO_INCREMENT, emp_id varchar(50) NOT NULL, emp_name varchar (250) NOT NULL, emp_email varchar (250) NOT NULL, emp_dept longtext NOT NULL, PRIMARY KEY id(id) )$charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } if(!class_exists('WP_List_Table')) { require_once(ABSPATH . 'wp-admin/includes/screen.php'); require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); } class Employees_List_Table extends WP_List_Table{ private $users_data; private function get_users_data($search = "") { global $wpdb; $table_name = $wpdb->prefix . 'ems'; if (!empty($search)) { return $wpdb->get_results( "SELECT * FROM $table_name WHERE id Like '%{$search}%' OR emp_id Like '%{$search}%' OR emp_name Like '%{$search}%' OR emp_email Like '%{$search}%'", ARRAY_A ); }else{ return $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A); } } // Define table columns function get_columns() { $columns = array( 'cb' => '<input type="checkbox" />', 'id' => 'ID', 'emp_id' => 'Employee ID', 'emp_name' => 'Employee Name', 'emp_email' => 'Email' ); return $columns; } // Prepare table items function prepare_items() { // Condidtion Search if (isset($_POST['page']) && isset($_POST['s'])) { $this->users_data = $this->get_users_data($_POST['s']); } else { $this->users_data = $this->get_users_data(); } $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); // var_dump($columns); /* pagination */ //$per_page = 2; $per_page = $this->get_items_per_page('employees_per_page', 20); $current_page = $this->get_pagenum(); $total_items = count($this->users_data); /* pagination */ $this->users_data = array_slice($this->users_data, (($current_page - 1) * $per_page), $per_page); $this->set_pagination_args(array( 'total_items' => $total_items, // total number of items 'per_page' => $per_page // items to show on a page )); usort($this->users_data, array(&$this, 'usort_reorder')); $this->items = $this->users_data; } // END Prepare table items // bind data with column function column_default($item, $column_name) { switch ($column_name) { case 'id': case 'emp_id': case 'emp_name': case 'emp_email': return $item[$column_name]; default: return print_r($item, true); //Show the whole array for troubleshooting purposes } } function column_cb($item) { return sprintf( '<input type="checkbox" name="eployee[]" value="%s" />', $item['id'] ); } // To show bulk action dropdown function get_bulk_actions() { $actions = array( 'delete_all' => 'Delete', 'draft_all' => "Move to Draft" ); return $actions; } // Add sorting to columns protected function get_sortable_columns() { $sortable_columns = array( 'id' => array('id', true), 'emp_id' => array('emp_id', false), 'emp_name' => array('emp_name', false) ); return $sortable_columns; } // Sorting function function usort_reorder( $a, $b ) { // If no sort, default to title $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id'; // If no order, default to asc $order = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc'; // Determine sort order $result = strcmp( $a[$orderby], $b[$orderby] ); // Send final sort direction to usort return ( $order === 'asc' ) ? $result : -$result; } // Adding action buttons to column function column_id($item) { $actions = array( 'edit' => sprintf('<a href="?page=%s&action=%s&employee_id=%s">Edit</a>', $_REQUEST['page'], 'edit', $item['id']), 'delete' => sprintf('<a href="?page=%s&action=%s&employee_id=%s">Delete</a>', $_REQUEST['page'], 'delete', $item['id']), ); return sprintf('%1$s %2$s', $item['id'], $this->row_actions($actions)); } } // End class function toty_emp_display_admin_menu() { $toty_hook = add_menu_page('EMS', 'EMS', 'manage_options', 'emp-list', 'da_ems_list_callback'); // screen option add_action("load-$toty_hook", 'my_emp_tbl_add_options'); function my_emp_tbl_add_options() { global $empTable; $option = 'per_page'; $args = array( 'label' => 'Number of items per page:', 'default' => 20, 'option' => 'employees_per_page' ); add_screen_option($option, $args); $empTable = new Employees_List_Table(); }; add_submenu_page('emp-list', 'Employee List', 'Employee List', 'manage_options', 'emp-list', 'da_ems_list_callback'); add_submenu_page('emp-list', 'Add Employee', 'Add Employee', 'manage_options', 'add-emp', 'da_ems_add_callback'); //add menu for update add_submenu_page(null, 'Update Employee', 'Update Employee', 'manage_options', 'update-emp', 'da_emp_update_call'); } // End toty_emp_display_admin_menu add_action('admin_menu', 'toty_emp_display_admin_menu'); // get saved screen meta value add_filter('set-screen-option', 'my_emp_table_set_option', 10, 3); function my_emp_table_set_option($status, $option, $value) { return $value; } function da_ems_add_callback() { global $wpdb; $table_name = $wpdb->prefix . 'ems'; $msg = ''; if (isset($_REQUEST['submit'])) { $wpdb->insert("$table_name", [ "emp_id" => $_REQUEST['emp_id'], 'emp_name' => $_REQUEST['emp_name'], 'emp_email' => $_REQUEST['emp_email'], 'emp_dept' => isset($_REQUEST['emp_dept']) ? stripslashes(html_entity_decode($_REQUEST['emp_dept'])) : '' ]); if ($wpdb->insert_id > 0) { $msg = "Saved Successfully"; } else { $msg = "Failed to save data"; } } ?> <h4 id="msg"><?php echo $msg; ?></h4> <form method="post"> <p> <label>EMP ID</label> <input type="text" name="emp_id" placeholder="Enter ID" required> </p> <p> <label>Name</label> <input type="text" name="emp_name" placeholder="Enter Name" required> </p> <p> <label>Email</label> <input type="email" name="emp_email" placeholder="Enter Email" required> </p> <p> <label>Department</label> <!-- <input type="text" name="emp_dept" placeholder="Enter Department" required> --> <?php //$description_field = 'emp_dept'; // Replace with your field name $editor_id = 'emp_dept'; $description_args = array( 'media_buttons' => true, 'textarea_rows' => 12, 'textarea_name' => 'emp_dept', 'editor_class' => 'description-editor widefat', 'wpautop' => true ); wp_editor($emp_dept, $editor_id, $description_args); ?> </p> <p> <button type="submit" name="submit">Submit</button> </p> </form> <?php } // List EMP with action function da_ems_list_callback() { if (isset($_REQUEST['action'])){ if($_REQUEST['action'] == 'edit'){ global $wpdb; $table_name = $wpdb->prefix . 'ems'; $msg = ''; $id = isset($_REQUEST['employee_id']) ? intval($_REQUEST['employee_id']) : ""; if (isset($_REQUEST['update'])) { if (!empty($id)) { $wpdb->update( "$table_name", [ "emp_id" => $_REQUEST['emp_id'], 'emp_name' => $_REQUEST['emp_name'], 'emp_email' => $_REQUEST['emp_email'], 'emp_dept' => stripslashes(html_entity_decode($_REQUEST['emp_dept'])) ], ["id" => $id] ); $msg = 'Data updated'; } } $employee_details = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name where id = %d", $id), ARRAY_A); ?> <h4><?php echo $msg; ?></h4> <form method="post"> <p> <label>EMP ID</label> <input type="text" name="emp_id" placeholder="Enter ID" value="<?php echo $employee_details['emp_id']; ?>" required> </p> <p> <label>Name</label> <input type="text" name="emp_name" placeholder="Enter Name" value="<?php echo $employee_details['emp_name']; ?>" required> </p> <p> <label>Email</label> <input type="email" name="emp_email" placeholder="Enter Email" value="<?php echo $employee_details['emp_email']; ?>" required> </p> <p> <label>Department</label> <?php $content = ''; // sanitize_text_field($employee_details['emp_dept']); // Replace with your field name $content = isset($employee_details['emp_dept']) ? stripslashes(html_entity_decode($employee_details['emp_dept'])) : ''; //var_dump($content); $editor_id = 'edit_emp_dept'; $wp_editor_setting = array( 'textarea_name' => 'emp_dept', ); wp_editor($content, $editor_id, $wp_editor_setting); ?> </p> <p> <button type="submit" name="update">Update</button> </p> </form> <?php }else if($_REQUEST['action'] == 'delete'){ echo "delete"; }else{ include_once TOTY_PLUGIN_DIR_PATH . "/view/toty_table_render.php"; } }else{ include_once TOTY_PLUGIN_DIR_PATH . "view/toty_table_render.php"; } }
- This reply was modified 6 months, 4 weeks ago by Ty Chhoun.
Sorry for the late reply. Unfortunately, your source code is not only incomplete but also somewhat difficult to read. A file called “toty_table_render.php” is missing. Unfortunately, if I deactivate its including, I only see a blank page when I open the “EMS” menu item.
However, I can already see the problem. You are trying to map data sets using your own database table. Of course you can use WP_List_Table as a basis for this, but you also have to put in a lot of your own effort.
I think a better solution would be if you map your data sets as custom post types. This means you stay in the WordPress standard and don’t have to design so much of the interface yourself. You could record the individual information per record as a post meta. At least that’s how I approach such individual plugins and I would recommend it to you too.
Hi
I understand that you recommend with post type, but my purpose is newest and fresh to learn WP that why I follow some some document. and I don’t want most record stored in table post.
so I hope you can check my bellow and can help thanks.
bellow is code for “toty_table_render.php”
<?php
// Creating an instance
$empTable = new Employees_List_Table();
echo ‘<div class=”wrap”><h2>Employees List Table</h2>’;
// Prepare table
$empTable->prepare_items();
?>
<form method=”post”>
<input type=”hidden” name=”page” value=”emp-list” />
<?php
$empTable->search_box(‘search’, ‘search_id’);
// Display table
$empTable->display();
?>
</form>
<?php
echo ‘</div>’;I had to test this myself first, but I have found a solution.
Change
$hidden = array();
to
$hidden = get_hidden_columns(get_current_screen());
After that it should work.
Dear threadi
Thanks you so much for your kindness help me, I am very happy with your help.
Thank
Ty
- The topic ‘WP 6.5 WP_List_Table some issue with Screen Option’ is closed to new replies.