WordPress Plugin Development for Beginners

06 Jul 2011

WordPress Plugin Development for Beginners

The power of WordPress is its ability to be customized and expand the core functionality using plugins. Plugins utilize PHP as well as the default functions, hooks, filters, and actions that are shipped with WordPress. We’ll cover the basics of writing our own WordPress plugins from scratch, but I’m assuming you already understand the basics of PHP so I won’t be going into too much detail about syntax.

Defining the WordPress Plugin File

The plugin file doesn’t have a lot of restrictions, but there is one section of code that is required by all WordPress plugins. Immediately after the opening PHP tag, place a block comment with the following information in it. This will define the details about the plugin that are needed for WordPress to activate, deactivate, and run the plugin script.

Plugin Name: Name Of Your Plugin
Plugin URI: http://www.pluginpage.com
Description: A short description of the plugin, usually one line long.
Version: 1.0
Author: Author
Author URI: http://www.author.com

Technically, all that is needed from a plugin is the Plugin Name line. If you were to upload a .php file with only this code in it to the /wp-content/plugins/ directory of your WordPress install, you would be able to activate the plugin. It would not do anything, but it would activate nonetheless. Without this standard plugin header, WordPress will not recognize that this plugin exists and will not load it.

The Hooks

The magic behind the WordPress framework is the use of Hooks. At various points while WordPress compiles your page to send to the browser it checks to see if any activated plugins have requested to run a function at that time. If they have, then WordPress runs the functions and proceeds with the code. You can access these Hooks by creating and removing Actions and Filters using the following codes.

//adding actions or filters
add_action(‘HOOK_NAME’, ‘FUNCTION_NAME’);
add_filter(‘HOOK_NAME’, ‘FUNCTION_NAME’);

//removing actions or filters
remove_action(‘HOOK_NAME’, ‘FUNCTION_NAME’);
remove_filter(‘HOOK_NAME’, ‘FUNCTION_NAME’);


The first type of Hook is called an action hook, and it is run at specific times during execution. For example, the WordPress action hook “the_header();” is run in the header of the html file. By tapping into this hook, we can add new style sheets or remove actions that are already running. WordPress prints several lines of code into the header of the theme files that many people choose to remove. By removing the actions that are set by default in the wp_head hook, we can remove the lines of code and reduce the amount of space in the header.

remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link' );


There is also a Filter Hook, which is used to modify outputs from WordPress. For instance, we could stop WordPress from writing the title of our posts, and instead wrap the title in “…” tags. Or, we could find all he places within the content of a post or page and add smiley faces wherever the text “mcp_smiley” shows up.

function mcp_smileyface_filter ($content) {
  $smiley = '<img src="';
  $smiley .= plugins_url( 'images/smiley.png' , __FILE__ );
  $smiley .= '" alt="smiley face" />';
  return preg_replace('/mcp_smiley/', $smiley, $content);

add_filter('the_content', 'mcp_smileyface_filter');

WordPress has many default functions similar to “plugins_url();” that help writing plugins. This particular function returns a URL to the plugins directory of the WordPress install. The parameters I passed locate the current plugin (__FILE__) and the smiley.png inside the images directory next to the current file.

It is good practice to prefix any and all functions or specific code with a unique identifier to your plugin. In this case, I put MCP (my initials) with a fairly good guarantee that no other plugin author would use the same prefix. This reduces the likeliness of your code being overwritten by another plugin developer.


So, we’ve created a .php file with the WordPress standard header to define our plugins name and other information about it. There were some default lines being printed in the header of our html so we removed WordPress default actions using the “remove_action()” function, and we added our custom filter to insert smiley faces into our posts where we wanted them.

Leave a Reply

Your email address will not be published. Required fields are marked *