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?
You get the error because of the line of $myListTable->prepare_items($additions);
. You pass $additions
While your function is not supporting a parameter:
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;
}
and the base function does not support a parameter either:
public function prepare_items() {
die( 'function WP_List_Table::prepare_items() must be overridden in a subclass.' );
}
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
inside prepare_items
. Basically one can do something like this:
class My_Cutsom_Table extends WP_List_Table {
//...
//This will store the additions you initially wanted to pass
protected $additions;
//Setter for additions
public function setAdditions($additions) {
$this->additions = $additions; return $this;
}
//Getter for additions
public function getAdditions() {
return $this->additions;
}
//...
public function prepare_items() {
//Your code here, you can get the additions via $this->getAdditions()
}
}
Usage:
$myListTable = new My_List_Table();
$myListTable->setAdditions($additions);
$myListTable->prepare_items();