我遇到了一些麻烦,主要是由于我对WordPress缺乏了解,我需要一些专家帮助。我正在从WordPress数据库中名为 wp_words 的表中检索数据,并以表格形式在WordPress管理员面板的菜单页面中显示它们。我创建了一个名为 Lexicon Testing 的菜单页面,该菜单页面中显示了表格。我想要做的是,当我单击每个代码下方的“编辑”按钮时(如图所示),以便能够编辑同一页面上的其余行字段。就像帖子和页面中的快速编辑功能一样。下面的代码显示了我检索数据的方式。我想要一些建议和指导。有可能像我想要的那样做吗?我是否需要以其他方式检索数据以使其那样工作?我用来检索数据的方式在此页面上:https://www.sitepoint.com/using-wp_list_table-to-create-wordpress-admin-tables/
<?php
/*
Plugin Name: Testing v1
Description: Testing
Version: 1.0
*/
if (!class_exists('WP_List_Table')) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Lexicon_words_List extends WP_List_Table {
/** Class constructor */
public function __construct() {
parent::__construct([
'singular' => __('Lexicon word', 'sp'), //singular name of the listed records
'plural' => __('Lexicon words', 'sp'), //plural name of the listed records
'ajax' => false //does this table support ajax?
]);
}
/**
* Retrieve customers data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_lexicon_words($per_page = 5, $page_number = 1) {
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}lexicon_words";
if (!empty($_REQUEST['orderby'])) {
$sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']);
$sql .= !empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC';
}
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results($sql, 'ARRAY_A');
return $result;
}
/**
* Delete a customer record.
*
* @param int $id customer ID
*/
public static function delete_lexicon_word($id) {
global $wpdb;
$wpdb->delete(
"{$wpdb->prefix}lexicon_words", array('id' => $id)
);
}
/**
* Edit a customer record.
*
* @param int $id customer ID
*/
public static function edit_lexicon_word($id) {
}
/**
* Returns the count of records in the database.
*
* @return null|string
*/
public static function record_count() {
global $wpdb;
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}lexicon_words";
return $wpdb->get_var($sql);
}
/** Text displayed when no customer data is available */
public function no_items() {
_e('No words avaliable.', 'sp');
}
/**
* Render a column when no column specific method exist.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default($item, $column_name) {
switch ($column_name) {
case 'code':
case 'text':
case 'phrase':
case 'context':
case 'level':
case 'lang':
return $item[$column_name];
default:
return print_r($item, true); //Show the whole array for troubleshooting purposes
}
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']
);
}
/**
* Method for code column
*
* @param array $item an array of DB data
*
* @return string
*/
function column_code($item) {
$delete_nonce = wp_create_nonce('sp_delete_lexicon_word');
$edit_nonce = wp_create_nonce('sp_edit_lexicon_word');
$title = '<strong>' . $item['code'] . '</strong>';
$actions = [
'edit' => sprintf('<a href="?page=%s&action=%s&lexicon_word=%s&_wpnonce=%s">Edit</a>', esc_attr($_REQUEST['page']), 'edit', absint($item['id']), $edit_nonce),
'delete' => sprintf('<a href="?page=%s&action=%s&lexicon_word=%s&_wpnonce=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', absint($item['id']), $delete_nonce)
];
return $title . $this->row_actions($actions);
}
/**
* Associative array of columns
*
* @return array
*/
function get_columns() {
$columns = [
'cb' => '<input type="checkbox" />',
'code' => __('Code', 'sp'),
'text' => __('Text', 'sp'),
'phrase' => __('Phrase', 'sp'),
'context' => __('Context', 'sp'),
'level' => __('Level', 'sp'),
'column_6' => __('Column 6', 'sp'),
'column_7' => __('Column 7', 'sp'),
'column_8' => __('Column 8', 'sp'),
'column_9' => __('Column 9', 'sp'),
'column_10' => __('Column 10', 'sp'),
'column_11' => __('Column 11', 'sp'),
'column_12' => __('Column 12', 'sp'),
'column_13' => __('Column 13', 'sp'),
'column_14' => __('Column 14', 'sp'),
'column_15' => __('Column 15', 'sp'),
'column_16' => __('Column 16', 'sp'),
'lang' => __('Lang', 'sp')
];
return $columns;
}
/**
* Columns to make sortable.
*
* @return array
*/
public function get_sortable_columns() {
$sortable_columns = array(
'code' => array('code', true),
'text' => array('text', false)
);
return $sortable_columns;
}
/**
* Returns an associative array containing the bulk action
*
* @return array
*/
public function get_bulk_actions() {
$actions = [
'bulk-delete' => 'Delete'
];
return $actions;
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
/** Process bulk action */
$this->process_bulk_action();
$per_page = $this->get_items_per_page('lexicon_words_per_page', 5);
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args([
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page //WE have to determine how many items to show on a page
]);
$this->items = self::get_lexicon_words($per_page, $current_page);
}
public function process_bulk_action() {
//Detect when a bulk action is being triggered...
if ('delete' === $this->current_action()) {
// In our file that handles the request, verify the nonce.
$nonce = esc_attr($_REQUEST['_wpnonce']);
if (!wp_verify_nonce($nonce, 'sp_delete_lexicon_word')) {
die('Fatal Error');
} else {
self::delete_lexicon_word(absint($_GET['lexicon_word']));
// esc_url_raw() is used to prevent converting ampersand in url to "#038;"
// add_query_arg() return the current url
//$origUrl = esc_attr($_REQUEST['page']);
wp_redirect(esc_url_raw(add_query_arg(array('page' => 'lexicon_testing'), admin_url('admin.php'))));
exit;
}
}
if ('edit' === $this->current_action()) {
// In our file that handles the request, verify the nonce.
$nonce = esc_attr($_REQUEST['_wpnonce']);
if (!wp_verify_nonce($nonce, 'sp_edit_lexicon_word')) {
die('Fatal Error');
} else {
self::edit_lexicon_word(absint($_GET['lexicon_word']));
// esc_url_raw() is used to prevent converting ampersand in url to "#038;"
// add_query_arg() return the current url
//$origUrl = esc_attr($_REQUEST['page']);
wp_redirect(esc_url_raw(add_query_arg(array('page' => 'lexicon_testing'), admin_url('admin.php'))));
exit;
}
}
// If the delete bulk action is triggered
if (( isset($_POST['action']) && $_POST['action'] == 'bulk-delete' ) || ( isset($_POST['action2']) && $_POST['action2'] == 'bulk-delete' )
) {
$delete_ids = esc_sql($_POST['bulk-delete']);
// loop over the array of record IDs and delete them
foreach ($delete_ids as $id) {
self::delete_lexicon_word($id);
}
// esc_url_raw() is used to prevent converting ampersand in url to "#038;"
// add_query_arg() return the current url
wp_redirect(esc_url_raw(add_query_arg(array('page' => 'lexicon_testing'), admin_url('admin.php'))));
exit;
}
}
}
class SP_Plugin {
// class instance
static $instance;
// customer WP_List_Table object
public $lexicon_words_obj;
// class constructor
public function __construct() {
add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3);
add_action('admin_menu', [$this, 'plugin_menu']);
}
public static function set_screen($status, $option, $value) {
return $value;
}
public function plugin_menu() {
$hook = add_menu_page(
'Lexicon Testing', 'Lexicon Testing', 'manage_options', 'lexicon_testing', [$this, 'plugin_settings_page']
);
add_action("load-$hook", [$this, 'screen_option']);
}
/**
* Plugin settings page
*/
public function plugin_settings_page() {
?>
<div class="wrap">
<h2>Lexicon Database Management</h2>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<?php
$this->lexicon_words_obj->prepare_items();
$this->lexicon_words_obj->display();
?>
</form>
</div>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
/**
* Screen options
*/
public function screen_option() {
$option = 'per_page';
$args = [
'label' => 'Words',
'default' => 5,
'option' => 'lexicon_words_per_page'
];
add_screen_option($option, $args);
$this->lexicon_words_obj = new Lexicon_words_List();
}
/** Singleton instance */
public static function get_instance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
function edit_word_metabox() {
add_meta_box('editword_metabox', '$title', 'editword_metabox_callback', 'lexicon_testing');
}
function editword_metabox_callback() {
}
}
add_action('plugins_loaded', function () {
SP_Plugin::get_instance();
});