I wrote a custom plugin and trying to extend WP_List_Table
. Its worked fine in older WordPress versions, But when i upgraded to wordpress 6.0 am getting following erro.
My_Cutsom_Table::prepare_items($additions) must be compatible with WP_List_Table::prepare_items()
Following is my code
class My_Cutsom_Table extends WP_List_Table {
function get_columns()
{
$columns = array(
'display_name' => 'User',
'user_email' => 'Email',
'company' => 'Company',
);
return $columns;
}
public function prepare_items() {
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $registrations;
}
function get_sortable_columns() {
$sortable_columns = array(
'display_name' => array('display_name', false),
'user_email' => array('user_email', false),
'company' => array('school', false),
'created_at' => array('created_at', false)
);
return $sortable_columns;
}
function column_default($item, $column_name) {
if ($column_name == 'edit_delete') {
return '<a href="/wp-admin/?page=bsog_events_registrations_delete&event_registration_id=' . $item['id'] . '&event_id=' . $_GET['event_id'] . '">Delete</a> | <a href="/wp-admin/?page=bsog_events_registrations_update&event_registration_id=' . $item['id'] . '&event_id=' . $_GET['event_id'] . '">Edit</a>';
} else {
return $item[$column_name];
}
}
}
And outside the class there is function and calling prepare_method. COde is as follows
function additions() {
global $wpdb;
$order_by = "";
$order = "";
$event_id = $_GET['event_id'];
//$msg = $_GET['msg'];
if (isset($_GET['orderby'])) {
$order_by = "ORDER BY " . $_GET['orderby'];
}
if (isset($_GET['order'])) {
$order = $_GET['order'];
}
$event = get_post($event_id);
$table_name_reg = $wpdb->prefix . 'events_registrations';
$table_name_user = $wpdb->prefix . 'users';
$query = "SELECT * FROM `$table_name_reg` AS reg JOIN `$table_name_user` AS u ON `reg`.`user_id` = u.`ID` WHERE `reg`.`event_id`=%d $order_by $order";
$query = $wpdb->prepare($query, [$event_id]);
$additions = $wpdb->get_results($query, 'ARRAY_A');
$myListTable = new My_List_Table();
$myListTable->prepare_items($additions);
if ($wpdb->num_rows > 0) {
$i = 1;
include 'views/additions.php';
} else {
echo '<div class="wrap"><p>No followers</p></div>';
}
die(0);
}
What is the cause of this error and how can I resolve it?
2
Answers
You get the error because of the line of
$myListTable->prepare_items($additions);
. You pass$additions
While your function is not supporting a parameter:and the base function does not support a parameter either:
See https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-list-table.php
EDIT
I was asked in the comment section about a possible way to be able to use
$additions
insideprepare_items
. Basically one can do something like this:Usage:
I have an example code from the plugin i have written for customer report..(hope it helps)
Here, $udata is table heading names…
Note from(wordpress codex):
This class’s access is marked as private. That means it is not intended for use by plugin and theme developers as it is subject to change without warning in any future WordPress release.
https://developer.wordpress.org/reference/classes/wp_list_table/
So, always, make a local copy of wp table class.
then change class name(something like below)
EDIT: i have added the sample data for prepare_items() below:
just write a function and return the data for table as an array
Note: Your keys should be same as on the inside.
then in prepare_items()
change the funtion name:
Edit2:
or you can do something like this:
suppose your file name is my_class.php
then in my_class.php