在Wordpress管理控制台中超出查询

时间:2016-02-04 01:42:06

标签: php jquery mysql wordpress

我正在尝试构建一个插件来管理Wordpress中的自定义表。在这个插件中,我将编写3个函数。第一个从数据库中获取数据,如下面的屏幕截图所示。第二个功能是更新记录状态。最后一项功能是删除记录。

我的问题是我不知道如何将该功能链接到Action列中的链接。 Wordpress不允许我或我不知道如何发送查询来删除或更新记录。

我想如果我点击更新,我点击选择的记录的状态将被更改然后重新加载页面。如果我点击删除然后重新加载页面,它将删除该记录。我可以编写函数,但没有运气向它发送ID查询。

请帮忙。谢谢。

enter image description here

<?php
/*
Plugin Name: Manage Custom Table
Plugin URI: http://_adddress.com
Description: Testing Plugin
Author: XXXXXXXXXX
Version: 1.0
*/
require_once('functions/functions.php');
add_action('admin_menu','ManageCustomTable_admin_actions');
function ManageCustomTable_admin_actions() {
    add_options_page('Manage Custom Table','Manage Custom Table','manage_options',__FILE__,'ManageCustomTable_admin');
}
function ManageCustomTable_admin(){
    global $wpdb;

    $data = $wpdb->get_results  ("
                    SELECT
                        id,
                        school_id,
                        added_date,
                        campus_status
                    FROM
                        table_campus
                ");
    ?>
    <style>.notice-warning {display:none;}</style>
    <table class="widefat" style="margin:20px auto; width:97%;">
        <thead>
            <tr>
                <th>ID</th>
                <th>School ID</th>
                <th>Added Date</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>ID</th>
                <th>School ID</th>
                <th>Added Date</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
        foreach ($data as $data) {
            echo '<tr>';
            echo '<td>'. $data->id.'</td>';
            echo '<td>'. $data->school_id.'</td>';
            echo '<td>'. $data->added_date.'</td>';
            echo '<td>'. $data->campus_status.'</td>';
            echo '<td>'; 
            ?>
            <a href=#> Update </a>
            <a href=#> Delete </a>
            <?php
            echo '</td></tr>';
        }
        ?>
        </tbody>
    </table>
<?php
}
?>

1 个答案:

答案 0 :(得分:1)

只需创建一个php文件并将您的表单放在该文件中。然后在您的插件主php文件中包含您创建的文件。

说你创建了一个delete.php,update.php

然后在你的php主插件文件

<?php
/*
Plugin Name: Manage Custom Table
Plugin URI: http://_adddress.com
Description: Testing Plugin
Author: XXXXXXXXXX
Version: 1.0
*/
//some of your codes here

include( plugin_dir_path(__FILE__).'delete.php');
include( plugin_dir_path(__FILE__).'update.php');

删除和更新的锚标记应该有一个标识符。例如$data-id

<a href="<?php echo admin_url('admin.php?sc_update=trues&sc_id=1');?>" >Update</a>

<a href="<?php echo admin_url('admin.php?sc_delete=trues&sc_id=1');?>" >Delete</a>

<小时/> delete.php

<?php 

if(isset($_GET['sc_delete']) && isset($_GET['sc_id'])){
   //[delete query here][1]
   $delete = $wpdb->delete( 'table_campus', array( 'ID' => $_GET['sc_id'] ) );
   if(false != $delete){
   //redirect to where ever you want 
   }

}

<小时/> 的 update.php

检查您的标识符是否已设置

 <?php 
   if(isset($_GET['sc_update']) && isset($_GET['sc_id'])){
     //get the database info in using $wpdb based on $_GET['sc_id']
     //then populate the data in form below
    $data = $wpdb->get_results  ($wpdb->prepare("
                    SELECT
                        id,
                        school_id,
                        added_date,
                        campus_status
                    FROM
                        table_campus where id = %d
                "),$_GET['sc_id']);
      //let say you have the following form below as an example
    ?>
      <form style="margin-left: 300px;" method="post">
        <input type="hidden" name="sc_Id" value="<?php echo $data->id?>">
        <input type="text" name="sc_status" value="<?php echo $data-> campus_status;?>"/>
        <input type="submit" value="Update" name="sc_update_submit"/>
      </form>
    <?php
   } 
   //check if the update form is submitted
   if(issset($_POST['sc_update_submit'])){
     //[excute the update][1] 
     $update = $wpdb->update('table_campus', array('campust_status'=>$_POST['sc_status']),array('id'=>$_POST['sc_Id']));
     if(false != $update){
       //redirect to where ever you want
     }

   }

希望有所帮助。