One of my favorite websites these days is NETTUTS, they always seem to have fresh, interesting content everytime I visit their site. Today Cristian Lupu posted a tutorial that walks you through the process of building your own WordPress Plugin from scratch that I thought was very well written.
Basically the tutorial walks you through the process of connecting to an OSCommerce database and displaying products from that database on your WordPress Blog. They also show you how to build your own configuration page for the WordPress Admin panel, which was very helpful to me.
Introduction
WordPress is gaining more and more popularity each day, not just as a blogging platform but also as a basic CMS, thus improving and extending its basic functionality becoming a day-to-day necessity for a lot of developers. Fortunately, the WordPress developers have foreseen these needs and added the possibility of customizing the basic functionality by adding plugins. Basicaly, a WordPress plugin is a (more or less) stand-alone piece of code that can be executed in different sections and stages within a page or site.
In today’s tutorial we’ll be talking about creating a WordPress plugin that extracts and displays products from an external OSCommerce shop database. We will start by describing the file structure of a plugin and where it must be included in the WordPress structure, then we’ll be having a closer look at how to make our plugin visible for WordPress and integrating it with actions run by its frame. Next, we’ll be creating a configuration panel for our plugin to allow the site administrator to customize it to his/her needs. Once done, we’ll be implementing the front-end functions themselves that will interact with the OSCommerce database and extract the required data. Finally, we’ll be modifying the default template to display the extracted data in the sidebar. Excited? Let’s get started!
Getting Started
While it would be possible to follow this tutorial by simply reading through it, I would recommend installing WordPress on your computer and follow the tutorial implementing all the steps. For this, you’ll need a local server running on your machine, like XAMPP for instance. Once you have it running, download and install WordPress. You will find extensive information about the installation process and troubleshooting on the WordPress site. For this tutorial we will be using release 2.7
Further on, you will need to set up an OSCommerce shop on your machine. You can download the latest release here: http://www.oscommerce.com/solutions/downloads
Files & Folders
First, we’ll need to create our basic files and folder structure. WordPress stores its plugins in the wp-content/plugins/ folder. This is the place where we’ll be adding our files as well. Normally, if your plugin is going to be very simple, you will include all the code inside one single PHP file. In this case, you will simply store the file in the folder mentioned above. However, in our case, we are going to use two files (one for the main plugin file and one for implementing the administration page) therefore we’ll be putting all our files in a specific folder that we’ll name oscommerce_importer. Go ahead and create this folder.
Creating the Plugin File
Next, we must create our main plugin file. We’ll name it oscommerce_importer.php. You can really name it whatever you want, it doesn’t make any difference.
If you now open your WordPress administration panel and navigate to the Plugins sections, your screen will look something like this.
As you can see, there is not the slightest sign of our new plugin. It’s time to change that and tell WordPress that our file is going to implement a plugin. The process to do so is very simple. All we need to do is add a plugin specific information header to our newly created file. This standard header will look like this:
<span style="color: #606060"> 1:</span> 1. <?php<span style="color: #606060"> 2:</span> 2. <span style="color: #008000">/* </span><span style="color: #606060"> 3:</span> <span style="color: #008000">3. Plugin Name: OSCommerce Product Display </span><span style="color: #606060"> 4:</span> <span style="color: #008000">4. Plugin URI: http://www.orangecreative.net </span><span style="color: #606060"> 5:</span> <span style="color: #008000">5. Description: Plugin for displaying products from an OSCommerce shopping cart database </span><span style="color: #606060"> 6:</span> <span style="color: #008000">6. Author: C. Lupu </span><span style="color: #606060"> 7:</span> <span style="color: #008000">7. Version: 1.0 </span><span style="color: #606060"> 8:</span> <span style="color: #008000">8. Author URI: http://www.orangecreative.net </span><span style="color: #606060"> 9:</span> <span style="color: #008000">9. */</span><span style="color: #606060"> 10:</span> 0. ?><span style="color: #606060"> 11:</span> Simple enough, don’t you think? You can, of course, change the content of this header to your liking but make sure you keep all the lines, otherwise WordPress won’t correctly recognize your plugin.
If you refresh your administration panel’s plugin page, you’ll now see our plugin listed along with the other ones, click here for screenshot.
Working with Action Hooks
Our plugin is now shown in the administration panel so WordPress is aware of it. However, it doesn’t do anything as it contains nothing except of the information header. We are going to change this now.
WordPress offers a great way to include your plugin code in different places all over the template, be it physical positions within a page or logical positions within the process of building up a page that is going to be displayed. First, we are going to have a closer look at the second category, the logical positions, better known as action hooks.
Action Hooks
You can view action hooks as callback function. Whenever WordPress is executing a certain operation, like, for instance, displaying the page footer, it will allow your plugins to execute their own code that must be run at that exact moment.
For a better understanding, let’s consider a generic plugin called my_plugin that implements a function called mp_footer() that has to be run whenever the page footer is displayed. We will tell WordPress to call this function, at the moment of displaying the footer by using a special function called add_action():
<span style="color: #606060"> 1:</span> 1. <php add_action(<span style="color: #006080">'wp_footer'</span>, <span style="color: #006080">'mp_footer'</span>); ?>The add_action() function takes the action hook name as its first parameter and the name of the function that must be executed, as a second parameter. This function call will be added to your main plugin file (the one containing the information header), usually, right under the function code that needs to be executed (mp_footer() in our example). You will find the full list of available action hooks in the WordPress Codex.
We’ll be using action hooks in the next chapter, where we are going to build the administration page for our plugin.
Creating the Plugins Administration Page
We’ll start the implementation of the module by defining its configurable parameters and make these accessible to the site administrator. Let’s see what these configuration bits would be:
- Database settings
- database host
- database name
- database user
- database password
- Store settings
- store URL
- folder for the product images
First, we need the database host, name, user and password in order to be able to connect to it and extract the needed data. Second, we need some general data about the store like its URL and the folder where the product images are stored. We need this information in order to be able to build the links because the paths contained in the database are all relative the previously mentioned product image folder.
Now that we know what we want to include in the configuration panel, it’s time to implement it. We’ll start by creating a new menu item to access the page and we’ll place it inside the Settings menu. Remember our chat about the action hooks in the previous chapter? It’s time to use this feature.
If you’ll scroll over the list of action hooks, you’ll see that WordPress also provides one that gets called when the basic menu structure has been generated (admin_menu) so, this would be the optimal place to chime in and create our own menu item.
Now that we identified the action we are going to use, all we need is to define our own function that will be called when this action hook runs. We’ll call our function oscimp_admin_actions() where oscimp_ stands for oscommerce importer and is used to create a possibly unique function name that will not get mismatched with any other function within WordPress or any of its plugins. Let’s see how the code will look like:
<span style="color: #606060"> 1:</span> 1. function oscimp_admin_actions() {<span style="color: #606060"> 2:</span> 2.<span style="color: #606060"> 3:</span> 3. }<span style="color: #606060"> 4:</span> 4.<span style="color: #606060"> 5:</span> 5. add_action(<span style="color: #006080">'admin_menu'</span>, <span style="color: #006080">'oscimp_admin_actions'</span>);As you can see, we are creating our function oscimp_admin_actions() then associate it with the admin_menu action hook using the add_action() function. The next step would then be to add some code to our oscimp_admin_actions() function to actually create the new menu item.
As with most WordPress things, adding a new menu item is also very easy. It all boils down to calling a single function. We would like to add our new menu item to the Settings menu so, in this case the function we need is add_options_page(). We’ll add the code inside the oscimp_admin_actions() function.
<span style="color: #606060"> 1:</span> 1. function oscimp_admin_actions() {<span style="color: #606060"> 2:</span> 2. add_options_page(<span style="color: #006080">"OSCommerce Product Display"</span>, <span style="color: #006080">"OSCommerce Product Display"</span>, 1, <span style="color: #006080">"OSCommerce Product Display"</span>, <span style="color: #006080">"oscimp_admin"</span>);<span style="color: #606060"> 3:</span> 3. }<span style="color: #606060"> 4:</span> 4.<span style="color: #606060"> 5:</span> 5. add_action(<span style="color: #006080">'admin_menu'</span>, <span style="color: #006080">'oscimp_admin_actions'</span>);If you refresh your admin page, you’ll see the new menu item appear under Settings. Click here for Screenshot.
Each existing menu has its own function to be used to add sub-menu items. For instance, if we would like to add our sub-menu item to the Tools menu instead of Settings, we would use the add_management_page() function instead of add_options_page(). You can find more details about the available options in the Adding Administration Menus section of the WordPress Codex.
If we get back to the newly added code line, you’ll probably notice the last parameter. This is actually a function name that will be called when the newly added menu item is clicked on and will be used to build the administration page of our plugin. Next, we’ll be adding this new function. However, before proceeding we should stop for a moment and think about what will be implemented on this page.
We already defined the parameters we want to make configurable (database host, name, user, etc) so these will have to be included in a form in order to allow the user to send the data to the database. Once the form is defined, we’ll need a bit of code that extracts the sent data from the form and saves it to the database. Last but not least, we need some code to extract the existing data from the database (if any) and pre-populate the form with these values. As you can see, there are quite a few things to do so, it might be a good idea to separate this functionality to its own file. We’ll name the file oscommerce_import_admin.php. Now, go and create an empty file with the given name.
As already mentioned, we’ll have to create the function that will display our plugin configuration page (we named this function oscimp_admin()). The code inside this function will be included from our newly created PHP file, oscommerce_import_admin.php
<span style="color: #606060"> 1:</span> 1. function oscimp_admin() {<span style="color: #606060"> 2:</span> 2. include(<span style="color: #006080">'oscommerce_import_admin.php'</span>);<span style="color: #606060"> 3:</span> 3. }<span style="color: #606060"> 4:</span> 4.<span style="color: #606060"> 5:</span> 5. function oscimp_admin_actions() {<span style="color: #606060"> 6:</span> 6. add_options_page(<span style="color: #006080">"OSCommerce Product Display"</span>, <span style="color: #006080">"OSCommerce Product Display"</span>, 1, <span style="color: #006080">"OSCommerce Product Display"</span>, <span style="color: #006080">"oscimp_admin"</span>);<span style="color: #606060"> 7:</span> 7. }<span style="color: #606060"> 8:</span> 8.<span style="color: #606060"> 9:</span> 9. add_action(<span style="color: #006080">'admin_menu'</span>, <span style="color: #006080">'oscimp_admin_actions'</span>);If you now click on the link under the Settings menu, you will be directed to an empty page. This is because our oscommerce_import_admin.phpfile is still empty. Click here for screenshot.
Next, we are going to create our form. For this we’ll use the following code:
<span style="color: #606060"> 1:</span> 1. <div <span style="color: #0000ff">class</span>=<span style="color: #006080">"wrap"</span>><span style="color: #606060"> 2:</span> 2. <?php echo <span style="color: #006080">"<h2>"</span> . __( <span style="color: #006080">'OSCommerce Product Display Options'</span>, <span style="color: #006080">'oscimp_trdom'</span> ) . <span style="color: #006080">"</h2>"</span>; ?><span style="color: #606060"> 3:</span> 3.<span style="color: #606060"> 4:</span> 4. <form name=<span style="color: #006080">"oscimp_form"</span> method=<span style="color: #006080">"post"</span> action=<span style="color: #006080">"<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>"</span>><span style="color: #606060"> 5:</span> 5. <input type=<span style="color: #006080">"hidden"</span> name=<span style="color: #006080">"oscimp_hidden"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"Y"</span>><span style="color: #606060"> 6:</span> 6. <?php echo <span style="color: #006080">"<h4>"</span> . __( <span style="color: #006080">'OSCommerce Database Settings'</span>, <span style="color: #006080">'oscimp_trdom'</span> ) . <span style="color: #006080">"</h4>"</span>; ?><span style="color: #606060"> 7:</span> 7. <p><?php _e(<span style="color: #006080">"Database host: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_dbhost"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $dbhost; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: localhost"</span> ); ?></p><span style="color: #606060"> 8:</span> 8. <p><?php _e(<span style="color: #006080">"Database name: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_dbname"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $dbname; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: oscommerce_shop"</span> ); ?></p><span style="color: #606060"> 9:</span> 9. <p><?php _e(<span style="color: #006080">"Database user: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_dbuser"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $dbuser; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: root"</span> ); ?></p><span style="color: #606060"> 10:</span> 0. <p><?php _e(<span style="color: #006080">"Database password: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_dbpwd"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $dbpwd; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: secretpassword"</span> ); ?></p><span style="color: #606060"> 11:</span> 1. <hr /><span style="color: #606060"> 12:</span> 2. <?php echo <span style="color: #006080">"<h4>"</span> . __( <span style="color: #006080">'OSCommerce Store Settings'</span>, <span style="color: #006080">'oscimp_trdom'</span> ) . <span style="color: #006080">"</h4>"</span>; ?><span style="color: #606060"> 13:</span> 3. <p><?php _e(<span style="color: #006080">"Store URL: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_store_url"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $store_url; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: http://www.yourstore.com/"</span> ); ?></p><span style="color: #606060"> 14:</span> 4. <p><?php _e(<span style="color: #006080">"Product image folder: "</span> ); ?><input type=<span style="color: #006080">"text"</span> name=<span style="color: #006080">"oscimp_prod_img_folder"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php echo $prod_img_folder; ?>"</span> size=<span style="color: #006080">"20"</span>><?php _e(<span style="color: #006080">" ex: http://www.yourstore.com/images/"</span> ); ?></p><span style="color: #606060"> 15:</span> 5.<span style="color: #606060"> 16:</span> 6.<span style="color: #606060"> 17:</span> 7. <p <span style="color: #0000ff">class</span>=<span style="color: #006080">"submit"</span>><span style="color: #606060"> 18:</span> 8. <input type=<span style="color: #006080">"submit"</span> name=<span style="color: #006080">"Submit"</span> <span style="color: #0000ff">value</span>=<span style="color: #006080">"<?php _e('Update Options', 'oscimp_trdom' ) ?>"</span> /><span style="color: #606060"> 19:</span> 9. </p><span style="color: #606060"> 20:</span> 0. </form><span style="color: #606060"> 21:</span> 1. </div>Explaining the Code
If you are familiar with HTML and PHP, the code above will make some sense but, still, let us shortly walk through the lines.
- We start by creating a div with the class wrap. This is a standard WordPress class that will make our page look like any other page in the administration area.
- The form will be using the POST method to send data back to itself. This means that the form data will be received by the same page so, we can add the database update code to the same file.
- Next, there is a hidden field that will be used to determine whether the current page is displayed after the user has pressed the Update Options button or not. When the page receives the form data, the value of this field will be set to Y.
- The next lines will create the form input fields for the database and store settings. As you can easily see, the value parameters are be set by the content of PHP variables. We’ll talk about these soon.
- Now if you refresh the admin page, you’ll see our newly created form. However, pressing the Update Options button will have no effect other than refreshing the page and the form fields are empty. Click here for screenshot.
Handling the Data
Once the form is ready to go, we’ll take care of the form data handling itself, updating the database and retrieving existing option values from the database. For this, we’ll first have to decide whether the current page is displayed after the user has pressed the Update Options button or not. We’ll do this by analyzing the value of the form’s hidden field, oscimp_hidden. The following code will be added at the very beginning of our oscommerce_import_admin.php file, before the code for displaying the form:
<span style="color: #606060"> 1:</span> 1. <?php<span style="color: #606060"> 2:</span> 2. <span style="color: #0000ff">if</span>($_POST[<span style="color: #006080">'oscimp_hidden'</span>] == <span style="color: #006080">'Y'</span>) {<span style="color: #606060"> 3:</span> 3. <span style="color: #008000">//Form data sent </span><span style="color: #606060"> 4:</span> 4. } <span style="color: #0000ff">else</span> {<span style="color: #606060"> 5:</span> 5. <span style="color: #008000">//Normal page display </span><span style="color: #606060"> 6:</span> 6. }<span style="color: #606060"> 7:</span> 7. ?>Next, we’ll be handling the form data and update the plugin options in the database accordingly. For this we’ll be using the update_option() function. The first parameter of this function is the option name which will be sued later to uniquely identify this option and its value. The second parameter is the value to be assigned.
<span style="color: #606060"> 1:</span> 1. <?php<span style="color: #606060"> 2:</span> 2. <span style="color: #0000ff">if</span>($_POST[<span style="color: #006080">'oscimp_hidden'</span>] == <span style="color: #006080">'Y'</span>) {<span style="color: #606060"> 3:</span> 3. <span style="color: #008000">//Form data sent </span><span style="color: #606060"> 4:</span> 4. $dbhost = $_POST[<span style="color: #006080">'oscimp_dbhost'</span>];<span style="color: #606060"> 5:</span> 5. update_option(<span style="color: #006080">'oscimp_dbhost'</span>, $dbhost);<span style="color: #606060"> 6:</span> 6.<span style="color: #606060"> 7:</span> 7. $dbname = $_POST[<span style="color: #006080">'oscimp_dbname'</span>];<span style="color: #606060"> 8:</span> 8. update_option(<span style="color: #006080">'oscimp_dbname'</span>, $dbname);<span style="color: #606060"> 9:</span> 9.<span style="color: #606060"> 10:</span> 0. $dbuser = $_POST[<span style="color: #006080">'oscimp_dbuser'</span>];<span style="color: #606060"> 11:</span> 1. update_option(<span style="color: #006080">'oscimp_dbuser'</span>, $dbuser);<span style="color: #606060"> 12:</span> 2.<span style="color: #606060"> 13:</span> 3. $dbpwd = $_POST[<span style="color: #006080">'oscimp_dbpwd'</span>];<span style="color: #606060"> 14:</span> 4. update_option(<span style="color: #006080">'oscimp_dbpwd'</span>, $dbpwd);<span style="color: #606060"> 15:</span> 5.<span style="color: #606060"> 16:</span> 6. $prod_img_folder = $_POST[<span style="color: #006080">'oscimp_prod_img_folder'</span>];<span style="color: #606060"> 17:</span> 7. update_option(<span style="color: #006080">'oscimp_prod_img_folder'</span>, $prod_img_folder);<span style="color: #606060"> 18:</span> 8.<span style="color: #606060"> 19:</span> 9. $store_url = $_POST[<span style="color: #006080">'oscimp_store_url'</span>];<span style="color: #606060"> 20:</span> 0. update_option(<span style="color: #006080">'oscimp_store_url'</span>, $store_url);<span style="color: #606060"> 21:</span> 1. ?><span style="color: #606060"> 22:</span> 2. <div <span style="color: #0000ff">class</span>=<span style="color: #006080">"updated"</span>><p><strong><?php _e(<span style="color: #006080">'Options saved.'</span> ); ?></strong></p></div><span style="color: #606060"> 23:</span> 3. <?php<span style="color: #606060"> 24:</span> 4. } <span style="color: #0000ff">else</span> {<span style="color: #606060"> 25:</span> 5. <span style="color: #008000">//Normal page display </span><span style="color: #606060"> 26:</span> 6. }<span style="color: #606060"> 27:</span> 7. ?>The code above if pretty much self-explanatory but please note that here we are using the PHP variables we have previously mentioned while building the form. These variables will be updated with the current form data values and will be displayed in the form itself. Go, check it out! Refresh the configuration page and enter your OSCommerce database settings as well as your store parameters then press Update Options.
If everything was implemented like described above, you’ll see an Options saved success message and the form fields will contain the data you have just entered. Click here for a Screenshot.
Last but not least, we’ll need to pre-populate the form with the database data when the user opens the configuration page. For this, we’ll be using the get_option() function which retrieves the specified option from the database.
<span style="color: #606060"> 1:</span> 1. <?php<span style="color: #606060"> 2:</span> 2. <span style="color: #0000ff">if</span>($_POST[<span style="color: #006080">'oscimp_hidden'</span>] == <span style="color: #006080">'Y'</span>) {<span style="color: #606060"> 3:</span> 3. <span style="color: #008000">//Form data sent </span><span style="color: #606060"> 4:</span> 4. $dbhost = $_POST[<span style="color: #006080">'oscimp_dbhost'</span>];<span style="color: #606060"> 5:</span> 5. update_option(<span style="color: #006080">'oscimp_dbhost'</span>, $dbhost);<span style="color: #606060"> 6:</span> 6.<span style="color: #606060"> 7:</span> 7. $dbname = $_POST[<span style="color: #006080">'oscimp_dbname'</span>];<span style="color: #606060"> 8:</span> 8. update_option(<span style="color: #006080">'oscimp_dbname'</span>, $dbname);<span style="color: #606060"> 9:</span> 9.<span style="color: #606060"> 10:</span> 0. $dbuser = $_POST[<span style="color: #006080">'oscimp_dbuser'</span>];<span style="color: #606060"> 11:</span> 1. update_option(<span style="color: #006080">'oscimp_dbuser'</span>, $dbuser);<span style="color: #606060"> 12:</span> 2.<span style="color: #606060"> 13:</span> 3. $dbpwd = $_POST[<span style="color: #006080">'oscimp_dbpwd'</span>];<span style="color: #606060"> 14:</span> 4. update_option(<span style="color: #006080">'oscimp_dbpwd'</span>, $dbpwd);<span style="color: #606060"> 15:</span> 5.<span style="color: #606060"> 16:</span> 6. $prod_img_folder = $_POST[<span style="color: #006080">'oscimp_prod_img_folder'</span>];<span style="color: #606060"> 17:</span> 7. update_option(<span style="color: #006080">'oscimp_prod_img_folder'</span>, $prod_img_folder);<span style="color: #606060"> 18:</span> 8.<span style="color: #606060"> 19:</span> 9. $store_url = $_POST[<span style="color: #006080">'oscimp_store_url'</span>];<span style="color: #606060"> 20:</span> 0. update_option(<span style="color: #006080">'oscimp_store_url'</span>, $store_url);<span style="color: #606060"> 21:</span> 1. ?><span style="color: #606060"> 22:</span> 2. <div <span style="color: #0000ff">class</span>=<span style="color: #006080">"updated"</span>><p><strong><?php _e(<span style="color: #006080">'Options saved.'</span> ); ?></strong></p></div><span style="color: #606060"> 23:</span> 3. <?php<span style="color: #606060"> 24:</span> 4. } <span style="color: #0000ff">else</span> {<span style="color: #606060"> 25:</span> 5. <span style="color: #008000">//Normal page display </span><span style="color: #606060"> 26:</span> 6. $dbhost = get_option(<span style="color: #006080">'oscimp_dbhost'</span>);<span style="color: #606060"> 27:</span> 7. $dbname = get_option(<span style="color: #006080">'oscimp_dbname'</span>);<span style="color: #606060"> 28:</span> 8. $dbuser = get_option(<span style="color: #006080">'oscimp_dbuser'</span>);<span style="color: #606060"> 29:</span> 9. $dbpwd = get_option(<span style="color: #006080">'oscimp_dbpwd'</span>);<span style="color: #606060"> 30:</span> 0. $prod_img_folder = get_option(<span style="color: #006080">'oscimp_prod_img_folder'</span>);<span style="color: #606060"> 31:</span> 1. $store_url = get_option(<span style="color: #006080">'oscimp_store_url'</span>);<span style="color: #606060"> 32:</span> 2. }<span style="color: #606060"> 33:</span> 3. ?>You can test the code above by simply navigating to another page within the admin area and then retuning to this page by clicking the OSCommerce Product Display sub-menu item in the Setting menu. If everything goes well, you will see the form with all the fields pre-populated with the data you have entered. Click here for a screenshot.
With this last piece of code, we have finished implementing the plugin’s configuration page so, let’s review what has been done in this chapter:
- we defined what parameters need to be configured by the site administrator
- we added an action hook for when the menu is displayed in the administration panel to help us add a new sub-menu item for our plugin
- we have added a new sub-menu item to the Settings menu that will link to our plugin’s configuration page
- we have defined a function that will build the plugin’s configuration page and separated its code in a second PHP file
- we have built the form containing the user inputs for each of the configurable data bits
- we have built the database update function
- we have built a function that will pre-populate the form with the option values stored in the database
Creating the User Function
Well, everything went quite fine so far but our plugin is yet unusable because we haven’t implemented the part that will actually allow us to display the products in the front-end.
In order to allow our users to display the products in the front-end, we’ll need to declare a function that can be called from the template’s PHP code and which will return the HTML code to be inserted in the template. We are going to name this function oscimp_getproducts() and accept the number of products to be displayed as a function parameter. The function itself will be implemented in our plugin’s main file, oscommerce_import.php
<span style="color: #606060"> 1:</span> 1. function oscimp_getproducts($product_cnt=1) {<span style="color: #606060"> 2:</span> 2.<span style="color: #606060"> 3:</span> 3. }As you can see, we are assigning a default value to our function parameter thus allowing our users to call the function both with and without a parameter. If the function is called with a parameter, like oscimp_getproducts(3), it will display three products. If the function is called without a parameter, like oscimp_getproducts(), it will only display one product.
First thing in our function would be to establish a connection to the OSCommerce database. Thanks to our plugin configuration page, we now have all the information we need: database host, name, user and password. We’ll be using the built-in wpdb class to create a new database object.
<span style="color: #606060"> 1:</span> 1. function oscimp_getproducts($product_cnt=1) {<span style="color: #606060"> 2:</span> 2. <span style="color: #008000">//Connect to the OSCommerce database </span><span style="color: #606060"> 3:</span> 3. $oscommercedb = <span style="color: #0000ff">new</span> wpdb(get_option(<span style="color: #006080">'oscimp_dbuser'</span>),get_option(<span style="color: #006080">'oscimp_dbpwd'</span>), get_option(<span style="color: #006080">'oscimp_dbname'</span>), get_option(<span style="color: #006080">'oscimp_dbhost'</span>));<span style="color: #606060"> 4:</span> 4. }Once this is done, we declare a variable that will contain the HTML code and start quering the OSCommerce database for each of the specified number of products. The code below merely implements this query loop and can be further-on improved by checking for duplicates, for instance, but this is not the subject of this tutorial so, we’ll keep it simple for the sake of readability.
<span style="color: #606060"> 1:</span> 1. function oscimp_getproducts($product_cnt=1) {<span style="color: #606060"> 2:</span> 2. <span style="color: #008000">//Connect to the OSCommerce database </span><span style="color: #606060"> 3:</span> 3. $oscommercedb = <span style="color: #0000ff">new</span> wpdb(get_option(<span style="color: #006080">'oscimp_dbuser'</span>),get_option(<span style="color: #006080">'oscimp_dbpwd'</span>), get_option(<span style="color: #006080">'oscimp_dbname'</span>), get_option(<span style="color: #006080">'oscimp_dbhost'</span>));<span style="color: #606060"> 4:</span> 4.<span style="color: #606060"> 5:</span> 5. $retval = <span style="color: #006080">''</span>;<span style="color: #606060"> 6:</span> 6. <span style="color: #0000ff">for</span> ($i=0; $i<$product_cnt; $i++) {<span style="color: #606060"> 7:</span> 7. <span style="color: #008000">//Get a random product </span><span style="color: #606060"> 8:</span> 8. $product_count = 0;<span style="color: #606060"> 9:</span> 9. <span style="color: #0000ff">while</span> ($product_count == 0) {<span style="color: #606060"> 10:</span> 0. $product_id = rand(0,30);<span style="color: #606060"> 11:</span> 1. $product_count = $oscommercedb->get_var(<span style="color: #006080">"SELECT COUNT(*) FROM products WHERE products_id=$product_id AND products_status=1"</span>);<span style="color: #606060"> 12:</span> 2. }<span style="color: #606060"> 13:</span> 3.<span style="color: #606060"> 14:</span> 4. <span style="color: #008000">//Get product image, name and URL </span><span style="color: #606060"> 15:</span> 5. $product_image = $oscommercedb->get_var(<span style="color: #006080">"SELECT products_image FROM products WHERE products_id=$product_id"</span>);<span style="color: #606060"> 16:</span> 6. $product_name = $oscommercedb->get_var(<span style="color: #006080">"SELECT products_name FROM products_description WHERE products_id=$product_id"</span>);<span style="color: #606060"> 17:</span> 7. $store_url = get_option(<span style="color: #006080">'oscimp_store_url'</span>);<span style="color: #606060"> 18:</span> 8. $image_folder = get_option(<span style="color: #006080">'oscimp_prod_img_folder'</span>);<span style="color: #606060"> 19:</span> 9.<span style="color: #606060"> 20:</span> 0. <span style="color: #008000">//Build the HTML code </span><span style="color: #606060"> 21:</span> 1. $retval .= <span style="color: #006080">'<div class="oscimp_product">'</span>;<span style="color: #606060"> 22:</span> 2. $retval .= <span style="color: #006080">'<a href="'</span>. $store_url . <span style="color: #006080">'product_info.php?products_id='</span> . $product_id . <span style="color: #006080">'"><img src="'</span> . $image_folder . $product_image . <span style="color: #006080">'" /></a><br />'</span>;<span style="color: #606060"> 23:</span> 3. $retval .= <span style="color: #006080">'<a href="'</span>. $store_url . <span style="color: #006080">'product_info.php?products_id='</span> . $product_id . <span style="color: #006080">'">'</span> . $product_name . <span style="color: #006080">'</a>'</span>;<span style="color: #606060"> 24:</span> 4. $retval .= <span style="color: #006080">'</div>'</span>;<span style="color: #606060"> 25:</span> 5.<span style="color: #606060"> 26:</span> 6. }<span style="color: #606060"> 27:</span> 7. <span style="color: #0000ff">return</span> $retval;<span style="color: #606060"> 28:</span> 8. }Once this is done, all we have to do is insert the oscimp_getproducts() function call to the template. We’ll be displaying three products at the bottom of the sidebar so, we are going to modify the sidebar.php file of our template, inserting the following code right below the list item containing the meta links:
<span style="color: #606060"> 1:</span> 1. <li><?php echo oscimp_getproducts(3); ?></li>If you refresh your front-end page now, you’ll see the three random products displayed at the bottom of the sidebar. Click here for a screenshot.
We have now implemented a WordPress plugin from scratch. Let’s summarize what has been done:
- we defined the way we store our plugin files
- we defined the information header in order to make our plugin visible for WordPress
- we talked about the action hooks and the way these are used
- we defined what parameters need to be configured by the site administrator
- we added an action hook for when the menu is displayed in the administration panel to help us add a new sub-menu item for our plugin
- we have added a new sub-menu item to the Settings menu that will link to our plugin’s configuration page
- we have defined a function that will build the plugin’s configuration page and separated its code in a second PHP file
- we have built the form containing the user inputs for each of the configurable data bits
- we have built the database update function
- we have built a function that will pre-populate the form with the option values stored in the database
- we have built our user function for use in the template
- we connected to the OSCommerce database
- we queried the OSCommerce database extracting the product ID, image and name
- we have built the HTML code for displaying the extracted data
- we have included the user function to the template sidebar
I hope this tutorial gave you all the information you need to build a WordPress plugin from the beginning. Please feel free to post your comments below.
Create a Custom WordPress Plugin From Scratch – NETTUTS
Questions or Comments?