I am trying to make a custom WP_List_Table
extension to display a custom post type.
Here is what the table and the DOM look like. You can see that it is rendering the table rows empty, and no header. There should be two posts so that much is working.
Here is the code for the page:
<?php
function customer_list_page() {
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
class CustomerList extends WP_List_Table {
public function __construct() {
parent::__construct([
'singular' => __('Customer', 'text-domain'),
'plural' => __('Customers', 'text-domain'),
'ajax' => false
]);
}
public static function get_customers() {
$query = new WP_Query(array(
'post_type' => 'customer'
));
$customer_posts = $query->posts;
$customers = array();
foreach($customer_posts as $customer_post) {
$new_customer = array();
$new_customer['postId'] = $customer_post->ID;
$new_customer['firstName'] = get_post_meta($customer_post->ID, 'lws-first-name', true);
$new_customer['lastName'] = get_post_meta($customer_post->ID, 'lws-last-name', true);
$new_customer['notes'] = get_post_meta($customer_post->ID, 'lws-notes', true);
$new_customer['customerId'] = get_post_meta($customer_post->ID, 'lws-customer-id', true);
$new_customer['email'] = get_post_meta($customer_post->ID, 'lws-email', true);
$new_customer['phone'] = get_post_meta($customer_post->ID, 'lws-phone', true);
$new_customer['mobile'] = get_post_meta($customer_post->ID, 'lws-mobile', true);
$new_customer['emailNotifications'] = get_post_meta($customer_post->ID, 'lws-email-notifications', true);
$new_customer['acceptChecks'] = get_post_meta($customer_post->ID, 'lws-accept-checks', true);
$new_customer['altContact'] = get_post_meta($customer_post->ID, 'lws-alt-contact', true);
$new_customer['altPhone'] = get_post_meta($customer_post->ID, 'lws-alt-phone', true);
$new_customer['street'] = get_post_meta($customer_post->ID, 'lws-street', true);
$new_customer['city'] = get_post_meta($customer_post->ID, 'lws-city', true);
$new_customer['state'] = get_post_meta($customer_post->ID, 'lws-state', true);
$new_customer['zip'] = get_post_meta($customer_post->ID, 'lws-zip', true);
$new_customer['pets'] = json_decode(get_post_meta($customer_post->ID, 'lws-pets', true));
array_push($customers, $new_customer);
}
return $customers;
}
public static function delete_customer($id) {
wp_delete_post($id);
}
public static function record_count() {
return wp_count_posts('customer')->draft;
}
public function no_items() {
_e('No customers to display', 'text-domain');
}
function column_name($customer) {
$delete_nonce = wp_create_nonce('lws-delete-customer');
$title="<strong>" . $customer['firstName'] . ' ' . $customer['lastName'] . '</strong>';
$actions = [
'delete' => sprintf('<a href="https://wordpress.stackexchange.com/questions/360682/?page=%s&action=%s&customer=%s&_wpnonce=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', absint($customer['postId']), $delete_nonce)
];
return $title . $this->row_actions($actions);
}
function column_default($customer, $column_name) {
return $customer[$column_name];
}
function column_cb($customer) {
return sprintf(
'<input type="checkbox" name="bulk-delete[]" value="%s"/>', $customer['postId']
);
}
function get_columns() {
return [
'cb' => '<input type="checkbox" />',
'customerId' => __('Customer ID', 'text-domain'),
'name' => __('Name', 'text-domain'),
'email' => __('E-Mail', 'text-doamin'),
'phone' => __('Phone #', 'text-domain')
];
}
public function get_sortable_columns() {
return [
'customerId' => ['customerId', true],
'name' => ['name', false],
'email' => ['emial', false],
'phone' => ['phone', false]
];
}
public function get_bulk_actions() {
return [
'bulk-delete' => 'Delete'
];
}
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
$this->process_bulk_actions();
$per_page = $this->get_items_per_page('customers_per_page', 20);
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args([
'total_items' => $total_items,
'per_page' => $per_page
]);
$this->items = self::get_customers($per_page, $current_page);
}
public function process_bulk_action() {
if($this->current_action() === 'delete') {
$nonce = esc_attr($_REQUEST['_wpnonce']);
if(!wp_verify_nonce($nonce, 'lws-delete-customer')) {
die('What the heck');
}
else {
self::delete_customer(absint($_GET['customer']));
wp_redirect(esc_url(add_query_arg()));
exit;
}
}
if(
(isset($_POST['action']) && $_POST['action'] == 'bulk-delete') ||
(isset($_POST['action2']) && $_POST['action2'] == 'bulk-delete')
) {
$delete_ids = esc_sql($_POST['bulk-delete']);
foreach($delete_ids as $id) {
self::delete_customer($id);
}
wp_redirect(esc_url(add_query_arg()));
exit;
}
}
}
$customers_table = new CustomerList();
?>
<div class="wrap">
<h2>Customers</h2>
<form method="post">
<?php
$customers_table->prepare_items();
$customers_table->display();
?>
</form>
</div>
<?php
}
?>
I can’t figure it out from my debugging. As far as I can tell everything is nearly identical to what I followed here.