Managing data custom tables with plugin for WordPress 3

Once you have a custom table created by plugin or phpMyAdmin, you might want to manage it through the Dashboard. So, this tutorial will show you how to create a menu item in the settings menu of the Dashboard, use the WordPress’ wpdb class to manipulate the data of the custom table and create an interface for it which matches the Worpress UI.

Step 1: Create the plugin directory and file
Create a directory called “bor-manage-table” in the wp-content/plugin directory under the WordPress installation directory. In this directory, create a PHP file called “bor-manage-table.php”

Step 2: Create the header file
Open the file “bor-manage-table.php” and add this PHP comment block.

<?php
/*
Plugin Name: Managing data table
Plugin URI: http://blog.boroniatechnologies.com/plugin
Description: Plugin add, update and delete data from a custom table
Author: Lebasca
Version: 1.0
Author URI: http://blog.boroniatechnologies.com/
*/

Step 3: Add the menu item to the Settings Menu
Below the header, add the following code to create the sub-menu item in the Settings menu. The sub-menu item created is called “Bor Plugin”

add_action('admin_menu', 'bor_plugin_menu');
function bor_plugin_menu() {
if (is_admin()){
add_options_page('Bor Plugin Options', 'Bor Plugin', 'manage_options', 'bor-plugin', 'bor_plugin_options');
}
}

Step 4: Get info from the custom table
After the previous code, add the following function codes to get data from “wp_bor_software” table.

/**Get all the data from the tabe wp_bor_software**/

function bor_get_software() {
global $wpdb;
$software = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."bor_software ORDER BY id ASC");
return $software;
}/**Get an specific row from the table wp_bor_software**/
function bor_get_softwarerow($id) {
global $wpdb;
$the_software = $wpdb->get_results("SELECT * FROM " .$wpdb->prefix ."bor_software WHERE id='".$id."'");
if(!empty($the_software[0])) {
return $the_software[0];
}
return;
}

Step 5: Add content to the dashboard
Below the previous functions, add the following function code to add the fields of the custom table into the Administration Dashboard.

function bor_software_meta_box() {
global $edit_software;
?>
<p>Software: <input type="text" name="bor_software" value="<?php if(isset($edit_software)) echo $edit_software->software;?>" /></p>
<p>Developer: <input type="text" name="bor_developer" value="<?php if(isset($edit_software)) echo $edit_software->developer;?>" /></p>
<p>Type: <input type="text" name="bor_type" value="<?php if(isset($edit_software)) echo $edit_software->type;?>" /></p>
<p>License: <input type="text" name="bor_license" value="<?php if(isset($edit_software)) echo $edit_software->license;?>" /></p>
<?php
}

Step 6: Call functions to create the interface and manipulate the data
After the previous code, add the following function code to call the functions that will create the interface and manipulate the data.

function bor_plugin_options(){
/**Manipulate data of the custom table**/
bor_action();
if (empty($_GET['edit'])) {
/**Display the data into the Dashboard**/
bor_manage_software();
} else {
/**Display a form to add or update the data**/
bor_add_software();
}
}

Step 7: Manipulate the data
Below the previous function, add the following function code that manipulates the data according to the action specified.

function bor_action(){
global $wpdb;

/**Delete the data if the variable "delete" is set**/
if(isset($_GET['delete'])) {
$_GET['delete'] = absint($_GET['delete']);
$wpdb->query("DELETE FROM " .$wpdb->prefix ."bor_software WHERE id='" .$_GET['delete']."'");
}

/**Process the changes in the custom table**/

Step 8: Display the interface to add a new row

After the previous code, add the following function code to create the meta box.

/**Get an specific row from the table wp_bor_software**/

global $edit_software;
if ($software_id) $edit_software = bor_get_softwarerow($software_id);

/**create meta box**/
add_meta_box('bor-meta', __('Software Info'), 'bor_software_meta_box', 'bor', 'normal', 'core' );
?>

/**Display the form to add a new row**/
<div>
<div id="faq-wrapper">
<form method="post" action="?page=bor-plugin">
<h2>
<?php if( $software_id == 0 ) {
$tf_title = __('Add software');
}else {
$tf_title = __('Edit software');
}
echo $tf_title;
?>
</h2>
<div id="poststuff">
<?php do_meta_boxes('bor', 'normal','low'); ?>
</div>
<input type="hidden" name="bor_software_id" value="<?php echo $software_id?>" />
<input type="submit" value="<?php echo $tf_title;?>" name="bor_add_software" id="bor_add_software">
</form>
</div>
</div>
<?php
}

Step 9: Display the data of the custom table and allow to manipulate it
Below the previous function, add the following function code to add a form which display the data from the custom table and manipulate it.

function bor_manage_software(){
?>
<div>
<div id="icon-edit"><br></div>
<h2><?php _e('Bor Plugin Options') ?></h2>
<form method="post" action="?page=bor-plugin" id="bor_form_action">
<p>
<select name="bor_action">
<option value="actions"><?php _e('Actions')?></option>
<option value="delete"><?php _e('Delete')?></option>
</select>
<input type="submit" name="bor_form_action_changes" value="<?php _e('Apply')?>" />
<input type="button" value="<?php _e('Add a new software')?>" onclick="window.location='?page=bor-plugin&amp;edit=true'" />
</p>
<table cellpadding="0">
<thead>
<tr>
<th id="cb" style="" scope="col">
<input type="checkbox"/>
</th>
<th><?php _e('Software')?></th>
<th><?php _e('Developer')?></th>
<th><?php _e('Type')?></th>
<th><?php _e('License')?></th>
</tr>
</thead>
<tfoot>
<tr>
<th id="cb" style="" scope="col">
<input type="checkbox"/>
</th>
<th><?php _e('Software')?></th>
<th><?php _e('Developer')?></th>
<th><?php _e('Type')?></th>
<th><?php _e('License')?></th>
</tr>
</tfoot>
<tbody>
<?php
$table = bor_get_software();
if($table){
$i=0;
foreach($table as $software) {
$i++;
?>
<trkw2" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; line-height: 18.200000762939453px; font-family: Monaco, 'Lucida Console', monospace; color: rgb(32, 96, 160); white-space: nowrap;"><?php echo (ceil($i/2) == ($i/2)) ? "" : "alternate"; ?>">
<th scope="row">
<input type="checkbox" value="<?php echo $software->id?>" name="bor_id[]" />
</th>
<td>
<strong><?php echo $software->software?></strong>
<div>
<span><a href="?page=bor-plugin&amp;id=<?php echo $software->id?>&amp;edit=true">Edit</a> | </span>
<span><a href="?page=bor-plugin&amp;delete=<?php echo $software->id?>" onclick="return confirm('Are you sure you want to delete this software?');">Delete</a></span>
</div>
</td>
<td><?php echo $software->developer?></td>
<td><?php echo $software->type?></td>
<td><?php echo $software->license?></td>
</tr>
<?php
}
}
else{
?>
<tr><td colspan="4"><?php _e('There are no data.')?></td></tr>
<?php
}
?>
</tbody>
</table>
<p>
<select name="bor_action-2">
<option value="actions"><?php _e('Actions')?></option>
<option value="delete"><?php _e('Delete')?></option>
</select>
<input type="submit" name="bor_form_action_changes-2" value="<?php _e('Apply')?>" />
<input type="button" value="<?php _e('Add a new software')?>" onclick="window.location='?page=bor-plugin&amp;edit=true'" />
</p>

</form>
</div>
<?php
}
?>

Step 10: Test the plugin

Open Admin To Check !!!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s