FacebookTwitter

Creating a WordPress plugin for beginners

By on Jan 31, 2014 in Beginners, PHP

Share On GoogleShare On FacebookShare On Twitter

WordPress has become the most popular CMS platform out there. It is very reliable and easy to use, but most of its success is due to the fact that WordPress is one of the most dynamic platforms, it can be extended via open source plugins that are available for free, and also paid one. Any body can develop a plugin for WordPress, and it does not take much to create a WordPress plugin. This concise tutorial is aim to guide you through the steps of creating a WordPress plugin from scratch.

WordPress is PHP base which makes it easy to understand and grasp. PHP is such a popular language that almost any web developer is familiar with it in some way. This popular accessibility is key to WordPress success. Building a plugin is just a matter of understanding a file structure and begin able to follow well written documentation from the WordPress website itself. Thats yet another good thing about WordPress, it is packed with documentation like no other CMS.

Getting Started

Its possible to follow this simple tutorial by just reading through it; however, I recommend that you download and install WordPress in your localhost so you can follow the steps in creating the plugin. It should be fairly simple to get WordPress running in your local host. Just make sure you have a local server running in your machine, free version of MAMP has done the trick for me for quite some time now. But before I got into the MAC world, when I use to be a windows guy I used XAMPP which is a good option for windows users.

We are going to be creating a simple slider plugin. We are going to implement basic functionalities like adding images, adding a transition effect and the ability to add our slider to a page or post via short-code.

The File Structure

To create a WordPress plugin all it takes really is one single PHP file. But that will not be best practice. Since we want to adhere to best programming practices, it is best to create a directory to house all your plugin files.

WordPress stores its plugins in the wp-content/plugins/ folder. So let’s go ahead and create a folder here and let’s call it simple_slider.

Adding The Files

Next, we must add our main plugin file, which we will name simple_slider.php. You can really name it whatever you want, it doesn’t make any difference.

Let’s now add some standard comments to the file, to give WordPress some insight about our plug-in.

1
2
3
4
5
6
7
8
9
10
11
<?php
 
/*
  Plugin Name: Simple Slider
  Plugin URI: http://www.pluginwebsite.net
  Description: Simple slider plugin
  Author: Enmanuel Corvo
  Version: 1.0
  Author URI: http://www.authorwebsite.net
 */
?>

And now WordPress can identify your plugin, so if you look at the plugin section inside the admin area you will see your plugin listed there, just like the image bellow:

creating a wordpress plugin for beginers

Simple enough, right? Of course, you can change the content of this header to your liking but make sure you keep all the lines, otherwise WordPress won’t recognize your plugin.

Adding Functionality To The Plug-in

As of now our plug-in doesn’t do much. It just tells WordPress what to show on the plugin section, but no actual functionality is implemented yet, that is about to change.

The Admin Section

We will start in the admin section, this is where the users of the plug-in would be able to add the images and other configurations for the slider. Since we are building a “simple slider” our plug-in is not going to have much configurations. It will just have the option to add images, auto play, and some entrance effects (transitions).

So we that in mind, we now have an idea more or less of how the admin panel is going to look. So we will start implementing it. First we need to add a new menu item to access the admin panel. We will place this in the “Settings” section.

The way we do this is by relying on WordPress Action Hooks

It would be a good idea to read on WordPress Action Hooks to get an understanding of what it is and how it works. But for the matter of our plugin we can quickly summarize WordPress Action Hooks:

Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter. You can pass extra arguments to the hooks, much like you can with apply_filters(). This function works similar to apply_filters() with the exception that nothing is returned and only the functions or methods are called.

So now let’s go ahead and add this line of code to our plugin file:

1
<php add_action('wp_footer', 'mp_footer'); ?>
Enmanuel Corvo

Enmanuel Corvo

Software Engineer at LiveAnswer
Enmanuel Corvo is a Software Engineer experienced in Web Applications Development. Huge fan of WordPress and an active plug-in author who enjoys writing code and blog posts about anything related to technology.
Enmanuel Corvo

Latest posts by Enmanuel Corvo (see all)