个性化阅读
专注于IT技术分析

WordPress自定义表单在管理面板上提交数据视图?

我在网站上创建了一个自定义表单, 其中包含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管理面板上, 以便我可以分别删除它们。

我想这样:

我的管理面板列表

那么, 没有任何插件怎么办呢?


#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>
赞(0)
未经允许不得转载:srcmini » WordPress自定义表单在管理面板上提交数据视图?

评论 抢沙发

评论前必须登录!