我想寫一個自定義屏幕選項到我的一個子主題,但我似乎無法讓它工作。我經歷了很多我在谷歌上找到的文章,無論出於何種原因它都沒有顯示。希望有人能指出我正確的方向。提前致謝。Wordpress屏幕選項沒有顯示在自定義主題
以下是通過主題安裝創建自定義管理選項頁面的所有類。
class country_class {
public function __construct(){
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
add_action("load-lp_manage_countries", "lp_screen_options");
}
function lp_screen_options() {
global $pippin_sample_page;
$lp_manage_countries_page = "lp_manage_countries";
$screen = get_current_screen();
// get out of here if we are not on our settings page
if(!is_object($screen) || $screen->id != $lp_manage_countries_page)
return;
$args = array(
'label' => __('Countries per page'),
'default' => 25,
'option' => 'per_page'
);
add_screen_option('per_page', $args);
}
public function install_countries_page(){
add_menu_page('LP Countries', 'LP Countries', 'manage_options', 'lp_manage_countries', array($this, 'show_country_page'));
}
public function show_country_page(){
echo "<div class=\"wrap\">
<h2>Manage Countries</h2>
<form method=\"post\" action=\"options.php\">";
//Prepare Table of elements
$categories_list_table = new category_list_table();
$categories_list_table->prepare_items();
//Table of elements
$categories_list_table->display();
echo "<input type=\"submit\" value=\"Submit\">
</form>
</div>";
}
public function create_countries_table(){
global $wpdb;
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`image` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) $charset; ";
$wpdb->query($sql);
}
public function drop_countries_table(){
global $wpdb;
$sql = "DROP TABLE `countries`;";
$wpdb->query($sql);
}
}
class category_list_table extends WP_List_Table {
public function __construct(){
parent::__construct(array(
'singular' => 'Country',
'plural' => 'Countries',
'ajax' => false)
);
}
public function get_columns(){
return $columns = array(
'cb' => '<input type="checkbox" />',
'id' => __('ID'),
'name' => __('Name'),
'parent_id' => __('Parent ID'),
'image' => __('Image')
);
}
function column_name($item) {
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&country=%s">Edit</a>',$_REQUEST['page'],'edit',$item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&country=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
);
return sprintf('%1$s %2$s', $item['name'], $this->row_actions($actions));
}
public function get_sortable_columns(){
return $sortable = array(
'id' => false,
'name' => true
);
}
public function prepare_items(){
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
/* Prepare the query */
$query = "SELECT * FROM `countries`";
/** Process bulk action */
$this->process_bulk_action();
/* Order Parameters */
$orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
$order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
/* Pagination Params */
//Number of elements in your table?
$totalitems = $wpdb->query($query); //return the total number of affected rows
//How many to display per page?
$perpage = 30;
//Which page is this?
$paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
//Page Number
if(empty($paged) || !is_numeric($paged) || $paged<=0){ $paged=1; }
//How many pages do we have in total?
$totalpages = ceil($totalitems/$perpage);
//adjust the query to take pagination into account
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=' LIMIT '.(int)$offset.','.(int)$perpage;
}
/* Register pagination */
$this->set_pagination_args(array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage
)
);
/* register the columns */
$columns = $this->get_columns();
$_wp_column_headers[$screen->id]=$columns;
/* Get the items */
$this->items = $wpdb->get_results($query, 'ARRAY_A');
$hidden_columns = array();
$sortable_columns = array();
$this->_column_headers = array($columns, $hidden_columns, $sortable_columns, "name");
}
public function column_default($item, $column_name) {
return $item[$column_name];
}
public function no_items() {
return 'No countries avaliable.';
}
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
);
}
public function get_bulk_actions() {
$actions = [
'bulk-delete' => 'Delete'
];
return $actions;
}
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, 'lp_delete_country')) {
die('Go get a life script kiddies');
}
else {
self::delete_country(absint($_GET['country']));
wp_redirect(esc_url(add_query_arg()));
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_country($id);
}
wp_redirect(esc_url(add_query_arg()));
exit;
}
}
function delete_country($id) {
global $wpdb;
$wpdb->delete(
"countries",
[ 'ID' => $id ],
[ '%d' ]
);
}
}
我提出建議的修改,但我仍然沒有任何運氣。 – joeb