WordPress 自定义表单在管理面板上提交数据视图?
Posted
技术标签:
【中文标题】WordPress 自定义表单在管理面板上提交数据视图?【英文标题】:WordPress Custom Form submit data view on admin panel? 【发布时间】:2016-09-22 13:14:49 【问题描述】:我在我的网站上创建了一个自定义表单,其中包含 2 个字段和一个提交按钮。 看看我的表格:
<div class="control-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" name="name" placeholder="Your Name" class="form-control">
</div>
</div>
<div class="control-group">
<label class="control-label" for="number">Mobile</label>
<div class="controls">
<input type="number" name="number" placeholder="017xxxxxxxx" class="form-control">
</div>
</div>
<button type="submit" value="sub" name="sub" class="btn btn-success btn-block btn-sm marTop15">Submit!</button>
当网站访问者提交表单时,我想将提交的数据显示在我的 WordPress 管理面板上,以便我可以单独删除它们。
我想做这样的:
那么我怎么能在没有任何插件的情况下做到这一点呢?
【问题讨论】:
请提供更多信息、更多代码或更多上下文。你在哪里提交你的数据,我的意思是你在哪里存储你的数据?在任何帖子类型或自定义表格中?您的管理页面怎么样,是帖子类型列表还是自定义页面。如果您edit您的问题并在那里提供更多详细信息,而不是在这里发表评论,那就更好了。 我想将数据存储在自定义表中,并且我的管理页面是自定义的。只有管理员可以在管理面板上查看提交的数据。基本上我想在管理面板上创建一个菜单并在此菜单上显示这些数据在表格视图中。@mayeenul Islam This admin menu page 使用带有自定义循环的表结构完成。看看并将其与您的自定义表格合并。 我不明白如何使用它。看我有一个自定义页面,在我的问题顶部有一个表单。这个表单只有 2 个字段,名称和移动。网站访问者访问此页面并填写这两个字段并提交。就是这样。现在我想在 wordpress 管理面板上查看这些数据。所以请帮助我...@MayeenulIslam 【参考方案1】:你需要使用“WP_List_Table”类
<?php
if(!class_exists('WP_List_Table'))
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
class TT_Orders_List_Table extends WP_List_Table
function __construct()
parent::__construct( array(
'singular' => 'ID',
'plural' => 'ID',
'ajax' => false
));
function column_default($item, $column_name)
switch($column_name)
case 'user_firstname':
case 'user_login':
case 'user_email':
case 'paid_charge':
case 'additional_member':
case 'date':
return $item->$column_name;
default:
return print_r($item,true); //Show the whole array for troubleshooting purposes
function column_user_firstname($item)
$actions = array(
'delete' => sprintf('<a href="users.php?page=%s&action=delete&id=%s&wp_http_referer=%s">Delete</a>', $_REQUEST['page'], $item->id, 'wp-admin/users.php?page='.$_REQUEST['page']),
);
return sprintf( '%1$s <span style="color:silver">(ID:%2$s)</span>%3$s', /*$1%s*/ $item->user_firstname, /*$2%s*/ $item->id, /*$3%s*/ $this->row_actions($actions));
function column_cb($item)
return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', /*$1%s*/ $this->_args['singular'], $item->ID );
function get_columns()
$columns = array(
'cb' => '<input type="checkbox" />',
'user_firstname' => 'User Name',
'user_login' => 'User ID',
'user_email' => 'Email',
'paid_charge' => 'Payment',
'additional_member' => 'Additional Member',
'date' => 'Registered Date'
);
return $columns;
function get_sortable_columns()
$sortable_columns = array(
'user_firstname' => array('u.user_firstname',true),
'user_login' => array('uz.user_login',true),
'user_email' => array('uz.user_email',false),
'paid_charge' => array('ui.paid_charge',false),
'additional_member' => array('ui.additional_member',false),
'date' => array('ui.date',false)
);
return $sortable_columns;
function get_bulk_actions()
$actions = array(
'delete' => 'Delete'
);
return $actions;
function process_bulk_action()
global $wpdb;
if( 'delete'===$this->current_action() )
$id = $_REQUEST['id'];
$ids = '';
if(is_array($id))
foreach($id as $k=>$v)
$ids .= $v . ",";
$ids = substr($ids, 0, -1);
else
$ids = $id;
if($ids != '')
$wpdb->query( "DELETE FROM `".$wpdb->prefix."events3` WHERE id IN ($ids)" );
function prepare_items()
global $wpdb;
$query = "
SELECT ui.id, u.user_firstname, ui.paid_charge,ui.date, ui.additional_member, uz.user_email, uz.user_login
FROM ".$wpdb->prefix."events3 ui
INNER JOIN ".$wpdb->prefix."user_info u
ON ui.user_id=u.user_id
INNER JOIN ".$wpdb->prefix."users uz ON u.user_id = uz.ID
WHERE ui.user_id = u.user_id AND ui.status = 1";
//echo $query;
//exit;
$orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'DESC';
$order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
if(!empty($orderby) & !empty($order)) $query.=' ORDER BY '.$orderby.' '.$order;
$totalitems = $wpdb->query($query);
$perpage = 15;
$hidden = array();
$paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
if(empty($paged) || !is_numeric($paged) || $paged<=0 ) $paged=1;
$totalpages = ceil($totalitems/$perpage);
if(!empty($paged) && !empty($perpage))
$offset=($paged-1)*$perpage;
$query.=' LIMIT '.(int)$offset.','.(int)$perpage;
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
));
$columns = $this->get_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$this->items = $wpdb->get_results($query);
//echo "<pre>";
//print_r($this->items);
//exit;
if(!empty($this->items))
$items = array();
foreach($this->items as $item)
if($item->additional_member == "yes")
$item->additional_member = 'Yes';
else if($item->additional_member == 'no')
$item->additional_member = 'No';
$paid_charge = explode(".", $item->paid_charge);
$item->paid_charge = $paid_charge[0];
$items[] = $item;
$this->items = $items;
$ordersListTable = new TT_Orders_List_Table();
$ordersListTable->prepare_items();
?>
<div class="wrap">
<div id="icon-users" class="icon32"><br/></div>
<h2>Form Data</h2>
<form id="posts-filter" method="get">
<input type="hidden" name="post_type" value="<?php echo $_REQUEST['post_type'] ?>" />
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
<?php $ordersListTable->display(); ?>
</form>
</div>
【讨论】:
另外你需要在function.php中添加这段代码,比如add_action('admin_menu', 'single_evening_users3'); function single_evening_users3() add_submenu_page( 'users.php', 'Singles Evening 3', 'Singles Evening 3', 'manage_options', 'singles-evening-users-listing3', 'singles_evening_users3_callback' ); function singles_evening_users3_callback() require_once "includes/single-evening-users3.php";
以上是关于WordPress 自定义表单在管理面板上提交数据视图?的主要内容,如果未能解决你的问题,请参考以下文章