Last active
January 13, 2022 13:29
-
-
Save paulvandermeijs/9209651 to your computer and use it in GitHub Desktop.
Replace a list table in the WordPress admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add action for admin_head-edit-comments.php | |
/* | |
* This action is triggered by admin-header.php when the user visits | |
* edit-comments.php and will execute after $wp_list_table is initially defined. | |
*/ | |
add_action("admin_head-edit-comments.php", function() { | |
// Create a custom class that extends WP_Comments_List_Table | |
/* | |
* For this example I've defined the class inside the action callback. In a | |
* real world scenario I would probably put the class in its own seperate | |
* file and include that file here. | |
* | |
* Because the original $wp_list_table was already defined using an instance | |
* of WP_Comments_List_Table when the admin_head-edit-comments.php action is | |
* triggered I don't have to bother about including other files. If you | |
* would define your custom class somewhere outside the callback you might | |
* need to include these files: | |
* | |
* - wp-admin/includes/class-wp-list-table.php | |
* - wp-admin/includes/class-wp-comments-list-table.php | |
*/ | |
class MyCommentsListTable extends WP_Comments_List_Table { | |
// Replace the single_row() method from the original WP_Comments_List_Table | |
/* | |
* In this example I'll replace the single_row() method which is called | |
* for each row in the list table. | |
* | |
* To get a better understanding of all available methods you might want | |
* to take a look at the comments for the original WP_List_Table and the | |
* definitions of methods for WP_Comments_List_Table. | |
*/ | |
function single_row($comment) { | |
echo '<tr><td colspan="' . $this->get_column_count() . '">' . | |
"My Custom Row for comment with ID {$comment->comment_ID} by {$comment->comment_author}" . | |
'</td></tr>'; | |
} | |
} | |
// Add global $wp_list_table to current scope | |
global $wp_list_table; | |
// Create an instance of your custom list table class | |
$myListTable = new MyCommentsListTable; | |
// Copy all properties from the global list table to yours | |
foreach ($wp_list_table as $key => $value) | |
$myListTable->{$key} = $value; | |
// Replace the global list table with yours | |
$wp_list_table = $myListTable; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment