2016-02-04 36 views
2

我正在嘗試構建一個插件來管理Wordpress中的自定義表。在這個插件中,我要寫3個函數。首先從數據庫中提取數據,如下圖所示。第二個功能是更新記錄狀態。最後一個功能是刪除一條記錄。在Wordpress管理儀表板中執行查詢

我的問題是我不知道如何將該功能鏈接到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 
} 
?> 
+0

看看[處理表單](http://php.net/manual/en/tutorial.forms.php)。 – Kenney

+0

我知道如何處理表單。但在WordPress的儀表板。這是行不通的。我無法將查詢發送到頁面本身。 –

+0

我的代碼中沒有看到'

'? – Kenney

回答

1

只要創建一個PHP文件,並把你的表格插件的主要PHP文件內部的file.Then裏面包括您所創建的文件。

說你刪除創建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

檢查是否您的標識符isset

<?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 
    } 

    } 

希望有幫助。

+0

感謝您提供非常詳細的答案。我只是有一個問題。我可以在functions.php中編寫2個函數刪除和更新,只包含一個functions.php而不是2個文件?如果我想擴展插件。我想把所有的功能放在functions.php中。可能嗎? –

+0

你指的是主題functions.php文件嗎? –

+1

你可以在你的插件目錄中添加functions.php文件。然後不要忘記將它包含在主插件文件中。 –