Create a custom WordPress plugin

Create a custom WordPress plugin from scratch

This tutorial will describe the implementation of a WordPress plugin ranging from scratch. The plugin will hook up with an external OSCommerce database and display random products on your Wordpress site. It also implements a configuration page for the Wordpress admin panel.

Introduction                Use services

Wordpress is gaining more and more popularity every day, not even as a blogging platform but also as a basic CMS, thus becoming the essential day-to-day requirement for its development and for the spread of developers... Fortunately, Wordpress developers have anticipated these needs and added the likelihood to optimize basic functionality by adding plugins. Basically, a Wordpress plugin may be a (more or less) stand-alone piece of code that will be executed in various sections and steps within a page or site.

In today's tutorial, we'll mention creating a WordPress plugin, which extracts and displays products from an external OSCommerce shop database. we'll start by describing the file structure of a plugin and where it's to be included within the WordPress structure, then we'll get a better check out how our plugin looks for Wordpress and the way to integrate it with the functions travel by our frame will see. next, we'll allow site admins to customize it to their needs by creating a configuration panel for our plugin.
Once done, we'll implement front-end functions ourselves which will interact with the OSXer database and extract the specified data. Finally, we'll modify the default template to display the extracted data within the sidebar. Excited? let's get started! start End Products!

To begin

While it might be possible to follow this tutorial through just reading it, I might recommend installing Wordpress on your computer and following the tutorial to implement all the steps. For this, you'll need an area server running on your machine, like XAMPP for instance. Wordpress once you run it, download and install it. you'll find detailed information about the installation process and troubleshooting on the Wordpress site. For this tutorial, we'll use Release 2.7.

Files and folders

First of all, we'll get to create our basic files and folder structure. WordPress stores its plugins in wp-content / plugins / folder. this is often where we'll also add our files. Generally, if your plugin goes to be very easy, you'll include all the code inside one PHP file. during this case, you'd simply store the enter the folder mentioned above. However, in our case, we've two files(For the most plugin file and for implementing an administration page), so we'll be putting all our files during a specific folder that we'll name oscommerce_importer. plow ahead and make this folder.

Plugin file creation

Next, we'd like to make our main plugin file. we'll name it oscommerce_importer.php. you'll name whatever you actually want, there's no difference.

Working with action hooks
Our plugin is now featured within the administration panel in order that WordPress is conscious of it. However, it does nothing because it contains nothing but an information header. this is often getting to change now.
WordPress provides an excellent thanks to incorporate your plugin code in several locations on your template, be it within the method of making a page or page displaying physical positions within logical posts. First, we're getting to take a better check out the second category, the logical condition, better referred to as the action hook.

Action hook

You can see the action hook as a callback function. Whenever performing a selected operation, for instance, by displaying the page footer, it'll allow your plugin to execute its own code that ought to run at a particular moment.
For a far better understanding, let's consider a generic plugin called my_plugin that implements a function called mp_footer () whenever the page footer is displayed. we'll ask WordPress to call this function, when displaying the footer employing a special function called add_action ():
We will use the hook of action within the next chapter, where we are getting to build the administration page for our plugin.

Creating a plugin administration page

We will begin the implementation of the module by defining our configurable parameters and allowing site administrators to access them. let's examine what these configuration bits will be:


  • Database settings
  • Database host
  • Database name
  • Database user
  • Database password
  • Store settings
  • Store URL

Folder for product images

First of all, we'd like the database host, name, user and password to attach thereto and extract the specified data. Second, we'd like some general data about the shop like the URL and therefore the folder where product images are stored. we'd like this information to be ready to create a link because the paths within the database all belong to the preceding product image folder.
Now that we all know what we would like to incorporate within the configuration panel, it is time to implement it. we'll start by creating a replacement menu item to access the page and that we will place it inside the Settings menu. Remember our conversation about the action hook within the previous chapter? it's time to use this feature.
If you scroll down the list of action hooks of action, you'll see that Wordpress also provides one that's called when the essential menu structure is generated (admin_menu), so it might be the simplest place to be and make your own menu. item.

Now that we've identified the action we are getting to use, all we'd like to try to do is define our own function, which can be called when this action is hook run. we'll call our function oscimp_admin_actions (), where oscimp_ stands for Oscommerce importer and is perhaps used.

Previous
Next Post »