Category Archives: Blog

Home / Archive by category "Blog"

Installing SlidingMenu Android library and example


SlidingMenu is a helpful Android library for developers. It creates a side navigation like the Facebook app but it is not easy to setup the library nor the example. Here you will find the steps to make the library and example free of errors and ready to use.

Step 1: Import the library and example
Download the library as well as the app on GitHub (https://github.com/jfeinstein10/slidingmenu). Then, unzip the file and create a new project in Eclipse. Select Android Project from Existing Code and click Next. Click Browse and choose the folder where you unzip the SlidingMenu library. Check Copy projects into workspace and click Finish. You have imported the library and example but you have an X icon next to the SlidingMenu library project and an exclamation mark icon next to the example project.

Step 2: Fix errors in SlidingMenu library project
The following list describes the possible errors that you might have after importing the library:

  • Console error: Unable to resolve target ‘Google Inc.:Google APIs:16.

    Fix: Go to Properties for library and check Google APIs for the desired platform.

  • List of errors: ACTION_POINTER_INDEX_MASK cannot be resolved android.
    HONEYCOMB cannot be resolved or is not a field
    LAYER_TYPE_HARDWARE cannot be resolved or is not a field
    LAYER_TYPE_NONE cannot be resolved or is not a field
    MATCH_PARENT cannot be resolved or is not a field
    The method getLayerType() is undefined for the type View
    The method setLayerType(int, null) is undefined for the type View

    Fix: Change the min sdk version of the library manifest with a recent one.

Step 3: Fix errors in SlidingMenu example project
After importing the example, these are the possible errors:

  • Console error: Found 2 versions of android-support-v4.jar in the dependency list,but not all the versions are identical (check is based on SHA-1 only at this time). Jar mismatch! Fix your dependencies

    Fix: Identify which project has the latest version and copy it on the old version. Clean the projects and restart eclipse if the error messages still appear in the console tab.

  • List of errors: The method getSupportActionBar() is undefined for the type BaseActivity
    The method getSupportMenuInflater() is undefined for the type BaseActivity
    The method onCreateOptionsMenu(Menu) of type BaseActivity must override or implement a supertype method
    The method onOptionsItemSelected(MenuItem) in the type Activity is not applicable for the arguments
    The method onOptionsItemSelected(MenuItem) of type BaseActivity must override or implement a supertype method
    The method onOptionsItemSelected(MenuItem) of type ResponsiveUIActivity must override or implement a supertype method

    Fix: Open SlidingFragmentActivity class of the SlidingMenu library project and add the following code before declaring the class:

    import com.actionbarsherlock.app.SherlockFragmentActivity;

    Then, replace the class declaration:

    public class SlidingFragmentActivity extends FragmentActivity implements SlidingActivityBase {

    with:

    public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {

    Clean and build the project if the error messages still appear.

Tip:Make sure that your example project is referencing the ActionBarSherlock and SlidingMenu library contained in your workspace.

Summary: Although SlidingMenu is a helpful Open Source library for Android developers, setting up the library and the example are not easy to do. So, this article intends to save you time on researching each error that you get after importing the library and example files.

Site and blog terminology in WordPress Multisite


WordPress Multisite is a great feature because you save time with the installation and upgrade of WordPress, themes and plugins by having a collection of sites sharing the same WordPress installation. However, the lack of detailed documentation and the uses of different terms for the same purpose in the dashboard and the WPMU functions can lead a confusion at the time of programming.
The dashboard uses the term “site” for each sub-domain or sub-directory installed in the network.

On the other hand, WPMU functions use the term site for the network and the term blog for each sub-domain or sub-directory. With the description of the WPMU function get_current_site, you might think that this function will get information of your current site viewed.

<!--?php <br ?--> $site = get_current_site();
echo 'You are viewing the current site
ID: '
.$site-&gt;id
.'
Domain: '
.$site-&gt;domain
.'
Path: '
.$site-&gt;path
.'
Site name: '
.$site-&gt;site_name;
?&gt;

However, you get information of the network.

You are viewing the current site
ID: 1
Domain: localhost
Path: /wordpress/
Site name: My Blog Test Multisite

To get information of your current site viewed, the right function to use is get_blog_details but you need to pass the blog id as a parameter. If you don’t know the blog id, you have to use the global variable $blog_id.

Note: You can also get the blog id by using the function get_id_from_blogname. However, it doesn’t retrieve the id for the main site. It only works for the subdomain or sudirectories.

<!--?php <br ?--> global $blog_id;
$site = get_blog_details($blog_id);
echo 'You are viewing the current site
Site ID:'
.$site-&gt;site_id
.'
Blog ID: '
. $site-&gt;blog_id
.'
Domain: '
.$site-&gt;domain
.'
Path: '
.$site-&gt;path
.'
Blog name: '
.$site-&gt;blogname;
?&gt;

With the use of this function, you finally get the information of the current site viewed.

You are viewing the current site
Site ID:1
Blog ID: 2
Domain: localhost
Path: /wordpress/boroniatest/
Blog name: Boronia Test

You can also use the global variables to obtain the same information as get_current_site and get_blog_details WPMU functions.

Global Variable Functions
$current_site get_current_site
$current_blog get_current_blog

Moreover, you should be aware that the objects returning information for the site or blog don’t contain the same amount of variables but some variable names are the same as domain and path. For more detailed information, check those links:

Activation and deactivation within a class in WordPress plugin


When classes are used for developing WordPress plugins, the action and filter hooks are declared inside the class and they call class methods. So, you assume that activation and deactivation hooks will work the same way but they don’t. After searching and testing, there are two found solutions:

  1. Declare the activation or deactivation function outside of the class as well as the function to be executed. Look at this example:
    <code lang="php">
    class btClass {

    function __construct(){
    $this->setup_actions();
    }

    function setup_actions(){
    add_action(‘init’,array(&$this,’init’));
    }

    function init(){
    // do something
    }
    }

    $btClassPlugin = new btClass();
    register_activation_hook(__FILE__, array(‘btClass’,’bt_activate’));
    register_deactivation_hook(__FILE__, array(‘btClass’,’bt_deactivate’));

    function bt_activate(){
    // do something
    }

    function bt_deactivate(){
    // do something.
    }

  2. Declare the activation or deactivation function outside of the class and call the class method to be executed. Look at this example:
    <code lang="php">
    class btClass {

    function __construct(){
    $this->setup_actions();
    }

    function setup_actions(){
    add_action(‘init’,array(&$this,’init’));
    }

    function activate(){
    // do something
    }

    function init(){
    // do something
    }

    function deactivate(){
    // do something
    }
    }

    $btClassPlugin = new btClass();
    register_activation_hook(__FILE__, array(‘btClass’,’activate’));
    register_deactivation_hook(__FILE__, array(‘btClass’,’deactivate’));

Note: Uninstall hook has to be used the same as activation and deactivation hooks.

Conception of Place Login WordPress plugin


The idea of creating another login plugin started after I tested several login widgets and none of them met my total needs. My first and the most important need was to have the same layout for all the pages including the login pages (register, lost password and profile). Most of those plugins use the WordPress standard login pages that show the WordPress logo and the form. Although it is possible to replace the WordPress logo, the layout still looks different. Another option is to create custom pages for those login procedures but there are no too much tutorial and documentation out there. A second need was to be compatible with WordPress multisite and restrict access to registered users of the site. The tested plugins worked fine with WordPress multisite but they allow any registered user to access to the current site. A third and last need was to use a login button instead of a login form that will open a signup form once it is clicked. After searching a while in WordPress plugin directory, I was unable to find any plugin with the desired need.

At the beginning, I was hesitating to develop another login plugin and I was considering to create just the custom pages for new user registration, lost password and profile because I didn’t want to spend too much time on that. After finding just one tutorial and being unsuccessful creating the pages, I decided to invest part of my time in creating another login plugin with all my needs because I was planning to develop some plugins for WordPress after all.

To start developing the plugin, I came up with a name based on what my plugin does. But after attempting to submit my plugin to WordPress, I realized that the name was already taken by other plugin. So, I had to rethink on another name and change the directory and file names, and the classes inside the files. Then, I had to re-test everything to make sure the plugin worked as before. Therefore, it is better search for the proposed naming in the plugin directory on WordPress before programming. To develop this plugin, I used two plugins as a base code: sidebar login for the login validation and form and bbpres for the virtual login pages. For the modal form, I used jQuery UI dialog and jQuery redmond theme. To allow plugin users changing the theme for the login button and form, the jQuery theme was registered in the wp_enqueue_scripts hook. The details about changing the theme are explained in the Place Login User Guide.
Instead of using WordPress standard login pages, this plugin has its own default pages that fits with the blog template theme. Those pages have a template file stored in the template directory of this plugin and they can be modified if they are copied to the WordPress theme directory. Only the lost password page is identical than the WordPress standard login pages. The new registration user page allows you to enter the first name, last name and password in contrast to WordPress standard registration page. The downside of this page is not to verify the email as WordPress does but I will work on that for the next release. And the edit profile allows you to modify only the data asked in the user registration page different from the WordPress that allows you to edit much more user information and give you access to the WordPress administration dashboard.

Although creating this plugin was a long learning process and time consuming, those efforts were satisfied by having a login plugin that does what I needed. Moreover, I expect this plugin can help to other WordPress users who have similar login needs as I had it.

Conception of Column Posts WordPress plugin


During the design of my homepage, I searched for ideas on other websites and I saw some of them displaying content in multiple columns. It gave me the idea of wanting to show my posts in that style. So, it led me to search in the WordPress plugin directory and I found only one plugin that shows the categories in multiple columns with their posts. However, I wanted to show the posts in columns and not the categories in columns. These results caused me to do more search in Google and I found some articles and tutorials explaining ways to create or display post content in multiple columns. Some of them explained how to modify the theme template files but this is not what I wanted. With those facts, I tested other plugins that displayed the latest posts in other formats and none of them fitted with my needs. As I was planning to create some WordPress plugins, the lack of this capability gave me the idea for developing my first plugin.

As being my first time developing a WordPress plugin, I read a WordPress plugin development book that guided me on the plugin development steps. The first step was to name my plugin by choosing a name based on what my plugin does. As I did the research before, I knew that my chosen name for this plugin (Column Posts) was not taken by the similar plugin. To start coding the plugin, I used the similar plugin “Posts Per Cat” as a base code. I kept the option of displaying the categories in multiples columns from the base code as the second alternative and I add the option of displaying the posts in multiples columns as the default. For the categories section, I kept four options at the beginning: include category, exclude category, order categories by and only top level categories. Top level categories option was removed in the final update because the new update version doesn’t display children posts in the category parent. I integrated headlines and contents in one section called posts and I kept the most relevant options. For the styling section, I added several options for the most relevant components of the output plugin such as category font color, headline font color, font family, etc. Each color field has a color picker that uses a Excolor jQuery plugin. I chose this jQuery plugin for being the easiest to use and customize.

The purpose of creating Column Posts was created to give Worpress users the possibility to display in multiple columns their latest posts of all or specific categories. Hopefully, this plugin can be found useful and be used for many WordPress users.

Installing and configuring Views Nivo Slider in Drupal 7


Having a slideshow in your front page is an eye-catching way to display your most featured products or sevices. Nivo Slider is a great image slider tool that can be integrated in Drupal; however, there’s no a detailed tutorial on how to install and configure this slider tool in Drupal 7. So, this tutorial will show you how to install and configure Views Nivo Slider in Drupal 7 step by step.

Step 1: Install the dependency modules
Download the following modules from Drupal Modules:

Once you downloaded all these modules, unzip those files in the sites/all/modules under your Drupal installation directory.

Caution: Download the versions that starts with the number 7.

Step 2: Install Views Nivo Slider
Download Views Nivo Slider from Drupal Modules and unzip the file in the sites/all/modules under your Drupal installation directory.

Step 3: Enable the modules
Enable the modules for each category as indicated below:

Note: It is neccessary to enable Chaos Tools module but the other modules in this category are not mandatory.



Step 4: Create image styles
Go to Configuration and click on Image Styles under Media section. Click on “Add style” and enter the following information:

  • Image style name: sliderimage
  • Effect: Resize 600 x 280

Step 5: Create a content type
Go to Structure and click on Content Type. Then, click on “Add a content type” and enter the following information:

  • Name: Nivo Slider
  • Description: Home Page Slider

Once you entered the data in the textboxes, click on “Save and add fields” button. Then, add a new field for the image field. Your screen should look like the image below:

Step 6: Create a view
Go to Structure and click on Views. Then, click on “Add a new view” and enter the following information:

  • View Name: Nivo Slider
  • Uncheck “Create a page” and check “Create a block”
  • Block Title: Slider Views
  • Display format: Views Nivo Slider of titles(linked)

Once you entered the data, click on “Continue & Edit” button. Click on settings under Format section and configure as you like. For this tutorial, these are the setting chosen:

  • Image field style: sliderimage
  • Style: Default
  • Effect: fade
  • Direction Navigator: No
  • Control Navigator: Basic

Click on “Add” button next to Fields section and tick on the box Content:Slider image (Appears in: node:nivo_slider). For this tutorial, this field was configured as the image shown below:

Click on Fields under Show line and select Views Nivo Slider as the row style. Then, configure the row style options as indicated below:

  • Image Field: Slider Image
  • Title Field: Content: Title
  • Link Field: <None>

Click on “Add” button next to Filter Criteria section, tick on the box Content:Type and tick on the box Nivo Slider under Content Types section.
At the end of this step, your screen view should look like the image below:

Step 7: Add content
Go to Content and click on Add Content. Then, click on “Nivo Slider” and fill the blank boxes as you like. For this tutorial, two contents were added.

Step 8: Enable the block view
Go to Structure and click on Blocks. In the disabled area, look for the view created “View:Nivo Slider” and enable it by choosing the desired region in the drop-down box. For this tutorial, the block was added to the Content region.

To make the block appears only in the front page, click the link “Configure” next to the region drop-down box. Go to Visibility settings on the Pages section and select “Only the listed pages”. Then, enter “<front>” in the text box below of the chosen option. Your screen should look like the image below:

Summary: This tutorial showed you the steps to install and configure Views Nivo Slider in Drupal 7. You learned the dependency modules required for this module and the steps involved to have a slideshow in your front page.

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**/
    if(isset($_POST['bor_add_software']) and isset($_POST['bor_software']) and isset($_POST['bor_developer']) and isset($_POST['bor_type']) and isset($_POST['bor_license']) ) {   
        /**Add new row in the custom table**/
        $software = $_POST['bor_software'];
        $developer = $_POST['bor_developer'];
        $type = $_POST['bor_type'];
        $license = $_POST['bor_license'];

        if(empty($_POST['bor_software_id'])) {
            $wpdb->query("INSERT INTO " .$wpdb->prefix ."bor_software(software,developer,type,license) VALUES('" .$software ."','" .$developer."','" .$type."','" .$license ."');");
        } else {
        /**Update the data**/
            $software_id = $_POST['bor_software_id'];
            $wpdb->query("UPDATE " .$wpdb->prefix. "bor_software SET software='" .$software ."', developer='" .$developer ."', type='" .$type ."', license='" .$license ."' WHERE id='" .$software_id ."'");
        }
    }  
}

Step 8: Display the interface to add a new row
After the previous code, add the following function code to create the meta box.

function bor_add_software(){
    $software_id =0;
    if($_GET['id']) $software_id = $_GET['id'];

    /**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 class="wrap">
      <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" class="metabox-holder">
            <?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" class="button-secondary">
        </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 class="wrap">
  <div class="icon32" 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" class="button-secondary" value="<?php _e('Apply')?>" />
        <input type="button" class="button-secondary" value="<?php _e('Add a new software')?>" onclick="window.location='?page=bor-plugin&amp;edit=true'" />
    </p>
    <table class="widefat page fixed" cellpadding="0">
      <thead>
        <tr>
        <th id="cb" class="manage-column column-cb check-column" style="" scope="col">
          <input type="checkbox"/>
        </th>
          <th class="manage-column"><?php _e('Software')?></th>
          <th class="manage-column"><?php _e('Developer')?></th>
          <th class="manage-column"><?php _e('Type')?></th>
          <th class="manage-column"><?php _e('License')?></th>
        </tr>
      </thead>
      <tfoot>
        <tr>
        <th id="cb" class="manage-column column-cb check-column" style="" scope="col">
          <input type="checkbox"/>
        </th>
          <th class="manage-column"><?php _e('Software')?></th>
          <th class="manage-column"><?php _e('Developer')?></th>
          <th class="manage-column"><?php _e('Type')?></th>
          <th class="manage-column"><?php _e('License')?></th>
        </tr>
      </tfoot>
      <tbody>
        <?php
          $table = bor_get_software();
          if($table){
           $i=0;
           foreach($table as $software) {
               $i++;
        ?>
      <tr class="<?php echo (ceil($i/2) == ($i/2)) ? "" : "alternate"; ?>">
        <th class="check-column" scope="row">
          <input type="checkbox" value="<?php echo $software->id?>" name="bor_id[]" />
        </th>
          <td>
          <strong><?php echo $software->software?></strong>
          <div class="row-actions-visible">
          <span class="edit"><a href="?page=bor-plugin&amp;id=<?php echo $software->id?>&amp;edit=true">Edit</a> | </span>
          <span class="delete"><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" class="button-secondary" value="<?php _e('Apply')?>" />
        <input type="button" class="button-secondary" 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
Go to your WordPress Dashboard, click on the Settings menu and find the sub-menu item created “Bor Plugin”. Then, click on this sub-menu item and you should see a screen like the image below.

Summary: This tutorial gave you the basic idea how to manipulate the data of a custom table by creating a user interface into the Administration Dashboard. You learned the functions to add a menu item to an existing menu and the functions to enable you to add data to your existing content. You also learned to use the WordPress styles to match with its user interface.

Creating custom tables WordPress Plugin

One of the essential knowledge required to develop plugins is to create additional tables in the WordPress database. Those custom tables allow developers to store specific information needed for their plugins rather than using the option table.

This tutorial shows you with simple steps how to create your custom table when the plugin is installed and how to delete the custom table when the plugin is deleted.

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

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

<?php
/*
Plugin Name: Create table
Plugin URI: http://blog.boroniatechnologies.com/plugin
Description: Plugin creating tables
Author: Lebasca
Version: 1.0
Author URI: http://blog.boroniatechnologies.com/
*/

Step 3: Activate the plugin
Below the header, add the following code to call the plugin activation function which is triggered when the plugin is activated in WordPress. The activation function calls the function “bor_install” to create the custom table.

register_activation_hook( __FILE__, 'bor_install' );

Step 4: Create the custom table
After the previous code, add the following code to create the custom table. This function uses WordPress’ wpdb class to create the table “wp_bor_software”.

function bor_install()
{
    /**Create an instance of the database class**/
    global $wpdb;

    /**Set the custom table name with the wp prefix "wp_bor_software"**/
    $table_name = $wpdb->prefix . "bor_software";

    /**Execute the sql statement to create or update the custom table**/
    $sql = "CREATE TABLE " . $table_name . " (
            id INT(9) NOT NULL AUTO_INCREMENT,
            software text NOT NULL,
            developer text NOT NULL,
            type text NOT NULL,
            license text NOT NULL,
       UNIQUE KEY id (id)
    );"
;

    require_once(ABSPATH.'wp-admin/includes/upgrade.php');
    dbDelta($sql);

    /**Insert one record in the table by using the array method**/
    $ini_software = "Wordpress";
    $ini_developer = "Wordpress Foundation";
    $ini_type = "Blog";
    $ini_license = "GNU";

    $results = $wpdb->insert( $table_name, array( 'software' => $ini_software, 'developer' => $ini_developer, 'type' => $ini_type, 'license' => $ini_license ) );    

}

Step 5: Deactivate the plugin
Below the previous function, add the following code to call deactivation function which is triggered when the plugin is deactivated in WordPress. The activation function calls the function “bor_uninstall” to delete the custom table.

register_uninstall_hook( __FILE__, 'bor_uninstall' );

Step 6: Delete the custom table
After the previous code, add the following code to delete the custom table “wp_bor_software”>

function bor_uninstall(){

    /**Create an instance of the database class**/
    global $wpdb;

    /**Set the custom table name with the wp prefix "wp_bor_software"**/
    $db_table_name = $wpdb->prefix . "bor_software";

    /**Execute the sql statement to delete the custom table**/
    $sql = 'DROP TABLE IF EXISTS '.$db_table_name;
    $wpdb->query( $sql );
}

Step 7: Close the php syntax
Add the last code in your file “bor-table.php” to end the PHP syntax.

?>

Step 8: Test the creation of the custom table
Go to your WordPress Dashboard, click on the plugin link to see the plugin created “Create table” and click on Activate link to create the custom table. Then, go to your phpAdmin and check for the “wp_bor_software” table.

Step 9: Test the deletion of the custom table
Go to your WordPress Dashboard, click on the plugin link to see the plugin created “Create table”, click on Deactivate link and click on Delete link to delete the plugin files and the custom table. Before doing that, you might want to copy this plugin directory in another directory for future reference. Then, go to your phpAdmin and the “wp_bor_software” table is gone.

Summary: This tutorial gave you the basics on how to create custom tables with a plugin in WordPress. You learned the activate and deactivate functions and the wpdb class to manipulate the database.

About Boronia Technologies

Boronia Technologies is the business name of Catherine Lebastard, an IT professional with extensive experience in web development and database administration. Catherine chose the name of Boronia as she likes flowers and purple color. The logo of her business is inspired on Boronia flower plus digital elements incorated inside the flower.


Why you should use my services?

Because I can guide you to the right path and provide you what you are looking for at the right price according to your budget. Also, I am able to develop or debug an application with any programming language that you request because I have the ability to learn at extremely fast pace and adapt easily to progress in technology.