Interested in writing your own WordPress Plugin? Me too. I have written a couple but I am still learning. I'm writing this Instructable because the best way to learn is to teach and share knowledge so let's do this.
This will be a living Instructable for a while as I have a good but limited working knowledge and as I learn more I will provide more.
The tutorial files can be found later in this tutorial.
This Instructable is the result of wanting to expand upon 2 previous Instructables:
- Display Instructables on your site using PHP
- Display Instructables on your site with a WordPress Plugin. Adds shortcodes to display by username and/or keywords.
Step 1: What You Will Need
What You Will Need:
You won't need an in-depth knowledge of any one of these but knowing where to find the information is half the battle.
- An Editor
- I use NotePad++
- If you don't have a hosting provider you can find thumbdrive based webservers for development that include MySQL.
- At least a basic knowledge of PHP
- A basic understanding of WordPress
- What is a Post, Page, Option, Meta
- A basic understanding of the WordPress Codex
- https://developer.wordpress.org/reference/ or https://codex.wordpress.org/Category:WordPress_De...
- Codex and Plugin Development Intro
- The Database Diagram
Step 2: Increase Your Understanding of WordPress
More to come
The images above include the WordPress Database Diagram from the WordPress Codex.
Step 3: Rules & General Guidelines
Always turn on debugging when developing a plugin or theme. To do this you will need to modify the wp-config.php changing existing and/or adding the lines from below. When working with a site that is in production always turn debugging back off when done with troubleshooting for security reasons. There are others that you can add to log errors and queries but that can be covered at another time.
Always add a comment to a function to state what it does. This is most likely not the first or last plugin you will develop. As time passes your coding style will change and you will forget things over time. You won't always remember what a function does exactly by the name alone or what all it was intended for in the future so a brief comment will save you from some headaches. Examples below:
/* A comment for multiple lines
Such as a description */
// A single line comment
What's in a Name
When creating functions and adding values to be used throughout WordPress you want to make the name for these unique and meaningful. This is important because if your names conflict with a value in WordPress or another plugin out there WordPress will start throwing errors. You can do something as simple as adding a short name of the plugin to the front of a function name. Example below :
For this Instructable we will call our plugin "idw" (Instructables DIY WordPress Plugin) so I would name my function like the following.
Step 4: Anatomy of a WordPress Plugin
* Plugin Name: IDWPlugin
* Plugin URI: https://www.instructables.com/id/Instructables-Wor...
* Description: IDW or Instructables DIY WordPress Plugin, a how-to framework.
* Version: 0.1.0
* Author: MrRedBeard
* Author URI: https://www.instructables.com/id/Instructables-Wor...
* License: GPL2
/* Copyright 2014 MrRedBeard (email : firstname.lastname@example.org)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
FIRST THINGS FIRST
defined('ABSPATH') or die("No script kiddies please!"); //Prevents someone from accessing this php directly.
//These are examples only and not intended to be use function
//do or assign stuff
//Make calls to the WordPress backend
INSTALL UNINSTALL ACTIONS
Step 5: Create a Shortcode
Ok so the following adds a shortcode to WordPress to display either the Instructables robot or HELLO WORLD.
//Shortcode function that is called. You can use arguments to configure how or what is returned.
function idw_HelloWorldShortCode( $args, $content = null )
$a = shortcode_atts(array
( 'img' => 'true',
), $args );
$returnValue = '';
if($a['img'] == ‘true’)
$returnValue = 'img url';
$returnValue = 'HELLO WORLD';
//Shortcode action that calls the function above and inserts the shortcode into WordPress.
//Used by placing [idwHelloWorld idw=”true”] or [idwHelloWorld idw=”false”] in a page or post.
Step 6: Add a StyleSheet
Sometimes when your plugin adds content to a page you need to give it it's own style sheet because themes are not created equally. This is a fairly simple process and very little is needed.
Create a style sheet and place it in the root of the plugin folder. Then add the following.
//Add custom StyleSheetfunction
wp_enqueue_style( 'prefix-style', plugins_url('Style.css', __FILE__) );
//Add custom StyleSheet
Step 7: Add Options
This step will be written up soon. Until then know that there are a couple ways of doing this. You can add a single option name and value or pass an array of option names and values. You can see a list of the existing WordPress Option names and values by going to the admin screen then change /wp-admin/ of the URL to /wp-admin/options.php
Do not use this code yet
//Get existing value
$idw_OptionValue = get_option('idw_OptionName');
//Update existing value
add_option('idw_OptionName', $idw_OptionValue, '');
//Remove option from database for uninstall cleanup
Step 8: Add an Admin Menu Item & Page
This step will be written up in full soon.
//Load Plugin Admin HTML for plugin page function
echo “Instructables DIY WordPress Plugin Tutorial”;
//Add Admin Page
add_menu_page( 'IDW Menu Item', 'IDW Menu Item', 'manage_options', 'idw-options', 'idw_PluginAdminHTML' );
//Add Admin Plugin Menu
Step 9: The Tutorial Files
These files are contained in a zip file and will be updated on occasion. In order to use this you will upload the zip via the admin page. In order to publish changes it will be easier to replace the files via FTP but depending on what you change you will need to deactivate and activate the plugin each time. The deactivate and activate actions remove and add actions to the WordPress base.
Step 10: ReadMe.txt
This step will be written up soon.
This step gives a user of your plugin some more information but more importantly it is required in order to publish to WordPress.org.
The ReadMe.txt template from WordPress is here https://wordpress.org/plugins/about/readme.txt.
You can validate your ReadMe.txt here https://wordpress.org/plugins/about/validator/
Step 11: Publishing Your First Plugin to WordPress
Step 12: What Else Do You Want to See Added or Learn How to Do?
So is there something specific that you want added to this Instructable or something you want to learn how to do? Let me know by posting in the comments below.
Step 13: What Did You Create?
Share what you created by posting below. Want some help ask away.