Louis Tran Louis Tran - 1 month ago 8
jQuery Question

Excecute a query in Wordpress Admin Dashboard

I'm trying to build a plugin to manage a custom table in Wordpress. In this plugin I'm gonna write 3 functions. First one to fetch data from database as you can see in the screenshot below. The second function is to update record status. And the last function is to delete a record.

My problem is I don't know how to link the function to the links in Action column. Wordpress doesn't allow me or I don't know how to send a query to to delete or update records.

I want if I click on update, status of the record I click choose will be changed then reload the page. And if I click on delete then reload the page, it will delete that record. I can write the functions but no luck to send ID query to it.

Please help. Thank you.

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

Answer

Just create a php file and put your form inside that file.Then inside your plugin main php file include the file you have created.

say you create a delete.php,update.php

Then inside your php main plugin file

<?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');

your anchor tag for delete and update should have an identifier. eg the $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

check whether your identifier 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
     }

   }

Hope that helps.