优秀的插件案例,可以知道全部现代开发的大概情况:
![](https://liangdabiao.com/wp-content/uploads/2024/02/image-105.png)
![](https://liangdabiao.com/wp-content/uploads/2024/02/image-106.png)
代码目录:
![](https://liangdabiao.com/wp-content/uploads/2024/02/image-104.png)
0: 使用composer:
//composer.json
{
"require": {
"jaeger/querylist": "^4.2",
"jaeger/querylist-puppeteer": "^4.0",
"tightenco/collect": "8.78.0"
},
"require-dev": {
"symfony/var-dumper": "^5.0"
},
"autoload": {
"files": [
"src/Helpers/helpers.php",
"src/Helpers/helpers2.php",
"src/Controller/TaskController.php",
"includes/fatrat-apierror.php",
"includes/fatrat-spider.php",
"includes/fatrat-options.php",
"includes/fatrat-options-add-edit.php",
"includes/fatrat-data.php",
"includes/fatrat-data-detail.php",
"includes/fatrat-validation.php",
"includes/fatrat-kit.php",
"includes/fatrat-debugging.php"
]
}
}
1:WP_List_Table 的使用:
//WP_List_Table
//是WordPress中用于显示数据的类,例如用户、插件、评论或文章。该类包含几乎所有显示、排序、分页和搜索数据所需的方法。您可以通过创建一个继承自WP_List_Table的类来创建自定义的列表表格,以满足您的特定需求。通过定义一些方法和变量,您可以自定义列、准备数据、排序、添加操作、批量操作、分页、搜索和其他自定义功能。这个类提供了丰富的功能,可以帮助您构建适合您需求的自定义列表表格。
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
class TaskController extends WP_List_Table
{
protected $wpdb;
protected $table_post;
class FRC_Configuration_List_Table extends WP_List_Table
{
class FRC_Data_List_Table extends WP_List_Table
{
class FRC_Data_Detail_Table extends WP_List_Table
{
上面是插件里面的使用,下面展示完整demo:
// 确保WP_List_Table类可用
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
// 创建一个继承自WP_List_Table的自定义类
class My_List_Table extends WP_List_Table {
// 示例数据
var $example_data = array(
array('ID' => 1, 'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell', 'isbn' => '978-0982514542'),
// 其他示例数据...
);
// 定义要显示的列
function get_columns(){
$columns = array(
'booktitle' => 'Title',
'author' => 'Author',
'isbn' => 'ISBN'
);
return $columns;
}
// 准备数据
function prepare_items() {
$columns = $this->get_columns();
$hidden = array();
$sortable = array();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $this->example_data;
}
// 显示默认列
function column_default( $item, $column_name ) {
switch( $column_name ) {
case 'booktitle':
case 'author':
case 'isbn':
return $item[ $column_name ];
default:
return print_r( $item, true ); // 用于调试目的
}
}
}
// 创建一个实例并显示列表表格
$myListTable = new My_List_Table();
$myListTable->prepare_items();
$myListTable->display();
2: 建立新表和使用:
$sql =
"CREATE TABLE IF NOT EXISTS $table_post(
`id` int(11) NOT NULL AUTO_INCREMENT,
`option_id` int(11) NOT NULL,
`status` tinyint(5) NOT NULL DEFAULT '1',
`title` varchar(120) NOT NULL DEFAULT '',
`cover` varchar(191) NOT NULL DEFAULT '',
`content` mediumtext NOT NULL,
`link` varchar(191) NOT NULL DEFAULT '',
`post_id` int(11) NOT NULL DEFAULT '0',
`message` varchar(191) NOT NULL DEFAULT '',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `option_id` (`option_id`),
KEY `status` (`status`),
UNIQUE KEY `link` (`link`)
) $charset_collate; ";
dbDelta( $sql );
//使用frc_post:
$this->wpdb = getDb();
$this->table_post = $this->wpdb->prefix . 'frc_post';