ItecSoftware Logo

How To Create A WordPress Plugin Or Widget

Written by Peter Gilg on - like this:
wordpress plugin widget

This is a very simple Tutorial to create a Partner Links plugin. Just the basics to get you started quickly.

A WordPress plugin is a snippet of code that builds and extends on the functionality of WordPress, which can add to the inner workings such as an SEO extension for example, or it can add visual elements to the site, like a Facebook link. Widgets allow these code segments to be quickly and easily enabled or disabled within predefined areas such in newer themes, like a sidebar.

Prior to WP version 2, modifications and addons had to be hard coded into the source code or the theme. With today’s version and the ability to add plugins, we have a modular system of adding and removing functionality. This also makes upgrades a lot easier.

Here I show you how to create a very simple plugin, then enable it as a widget.

Create A WordPress plugin

Let’s create a new file in plugin inside your WordPress directory. Call it anything you like, I will use myplugin.php. Then we’ll add some code that creates partner links in your sidebar:

<?php
 /*
 Plugin Name: My Plugin
 Plugin URI: http://www.itecsoftware.com/
 Description: Partner Links WordPress Plugin
 Author: Peter
 Version: 1.0
 Author URI: http://www.itecsoftware.com/
 */
 function partnerLinks() {
 echo "<h2>Partner Links</h2>";
 }
 ?>

Code inside /* */ are called comments, and in this case are used by WordPress to display information to the webmaster or whoever installs and configures it. The function partnerLinks will display the title, nothing more at this point. Save the file and you now should have a new plugin visible in the WordPress plugin section.

Adding the WordPress Widget functionality

WordPress Widgets are based on the same foundation as plugins, but in addition they ease the handling of adding/removing software tremendously. By adding the widget functions, we will be able to simply click and drag it to the sidebar. Let’s append the code underneath our existing file:

<?php
 function widget_partnerLinks()  {
   ?>
   <h2>Partner Links</h2>
   <?php 
   partnerLinks();
 }
 function partnerLinks_init() {
  register_sidebar_widget(__('Partner Links'), 'widget_partnerLinks');
 }
 add_action("plugins_loaded", "partnerLinks_init");
 ?>

The first function widget_partnerLinks will display the title on the sidebar once its set-up correctly. Note that we are calling our initial function. We soft of upgraded the plugin to become a widget. We could have cleaned up our code by combining the two function, but I leave that up to you.

The second function is called by WordPress when the plugin is activated. It calls a WordPress function that will register a new widget called “Partner Links” which in turn will call our new widget function. All you have to add is a list of your partner’s url. All you have to do now is drag the widget onto a sidebar location and check it out on your site.

Listed in Web Development

Tags: plugin, sidebar, widget, wordpress

Leave a Reply