gpt4 book ai didi

wordpress - 在插件管理中扩展 WP_List_Table/处理复选框选项

转载 作者:行者123 更新时间:2023-12-04 14:39:05 25 4
gpt4 key购买 nike

我正在开发一个 WordPress 插件,该插件的一部分需要扩展 WP_List_Table 并将在该表中选中的任何项目存储到一个选项中。我已经设法弄清楚如何正确设置和显示所需的表格,但我该如何处理存储选中的选项?

这是我到目前为止所得到的......

class TDBar_List_Table extends WP_List_Table {

// Reference parent constructor
function __construct() {
global $status, $page;

// Set defaults
parent::__construct( array(
'singular' => 'theme',
'plural' => 'themes',
'ajax' => false
));
}

// Set table classes
function get_table_classes() {
return array('widefat', 'wp-list-table', 'themes');
}

// Setup default column
function column_default($item, $column_name) {
switch($column_name) {
case 'Title':
case 'URI':
case'Description':
return $item[$column_name];
default:
return print_r($item, true);
}
}

// Displaying checkboxes!
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="%1$s" id="%2$s" value="checked" />',
//$this->_args['singular'],
$item['Stylesheet'] . '_status',
$item['Stylesheet'] . '_status'
);
}

// Display theme title
function column_title($item) {
return sprintf(
'<strong>%1$s</strong>',
$item['Title']
);
}

// Display theme preview
function column_preview($item) {
if (file_exists(get_theme_root() . '/' . $item['Stylesheet'] . '/screenshot.png')) {
$preview = get_theme_root_uri() . '/' . $item['Stylesheet'] . '/screenshot.png';
} else {
$preview = '';
}
return sprintf(
'<a href="%1$s" class="thickbox" title="%2$s"><img src="%3$s" style="width: 150px;" /></a>',
$preview,
$item['Title'],
$preview
);
}

// Display theme description
function column_description($item) {
if (isset($item['Version'])) {
$version = 'Version ' . $item['Version'];
if (isset($item['Author']) || isset($item['URI']))
$version .= '&nbsp;|&nbsp;';
} else {
$version = '';
}
if (isset($item['Author'])) {
$author = 'By ' . $item['Author'];
if (isset($item['URI']))
$author .= '&nbsp;|&nbsp;';
} else {
$author = '';
}
if (isset($item['URI'])) {
$uri = $item['URI'];
} else {
$uri = '';
}

return sprintf(
'<div class="theme-description"><p>%1$s</p></div><div class="second theme-version-author-uri">%2$s%3$s%4$s',
$item['Description'],
$version,
$author,
$uri
);
}

// Setup columns
function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Theme',
'preview' => 'Preview',
'description' => 'Description'
);
return $columns;
}

// Make title column sortable
function get_sortable_columns() {
$sortable_columns = array(
'title' => array('Title', true)
);
return $sortable_columns;
}

// Setup bulk actions
function get_bulk_actions() {
$actions = array(
'update' => 'Update'
);
return $actions;
}

// Handle bulk actions
function process_bulk_action() {
// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}

if ('update' === $this->current_action()) {
foreach ($themes as $theme) {
if ($theme['Stylesheet'] . '_status' == 'checked') {
// Do stuff - here's the problem
}
}
}
}

// Handle data preparation
function prepare_items() {

// How many records per page?
$per_page = 10;

// Define column headers
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();

// Build the array
$this->_column_headers = array($columns, $hidden, $sortable);

// Pass off bulk action
$this->process_bulk_action();

// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}

// Handle sorting
function usort_reorder($a,$b) {
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'Title';
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
$result = strcmp($a[$orderby], $b[$orderby]);
return ($order === 'asc') ? $result : -$result;
}
usort($themes, 'usort_reorder');

//MAIN STUFF HERE
//for ($i = 0; i < count($themes); $i++) {

//}

// Figure out the current page and how many items there are
$current_page = $this->get_pagenum();
$total_items = count($themes);

// Only show the current page
$themes = array_slice($themes,(($current_page-1)*$per_page),$per_page);

// Display sorted data
$this->items = $themes;

// Register pagination options
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items/$per_page)
));
}
}

问题是,我无法正确保存它。我选择我想要的行,点击保存,它就会重置。

最佳答案

我假设您是在讨论表格列表中的复选框,所以这就是处理批量操作的方法。

您需要做的就是向您的类添加两个新方法,并在 prepare_items 方法中对其进行初始化。我在我的一个插件中使用下面的代码来删除或导出,但您也可以轻松地运行更新。

/**
* Define our bulk actions
*
* @since 1.2
* @returns array() $actions Bulk actions
*/
function get_bulk_actions() {
$actions = array(
'delete' => __( 'Delete' , 'visual-form-builder'),
'export-all' => __( 'Export All' , 'visual-form-builder'),
'export-selected' => __( 'Export Selected' , 'visual-form-builder')
);

return $actions;
}

/**
* Process our bulk actions
*
* @since 1.2
*/
function process_bulk_action() {
$entry_id = ( is_array( $_REQUEST['entry'] ) ) ? $_REQUEST['entry'] : array( $_REQUEST['entry'] );

if ( 'delete' === $this->current_action() ) {
global $wpdb;

foreach ( $entry_id as $id ) {
$id = absint( $id );
$wpdb->query( "DELETE FROM $this->entries_table_name WHERE entries_id = $id" );
}
}
}

现在,像这样在 prepare_items() 中调用这个方法:

function prepare_items() {
//Do other stuff in here

/* Handle our bulk actions */
$this->process_bulk_action();
}

有一个很棒的助手插件叫做 Custom List Table Example这使得弄清楚 WP_List_Table 类变得容易得多。

关于wordpress - 在插件管理中扩展 WP_List_Table/处理复选框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com