SpinSpire logo

Getting Started with Wordpress Plugin Development

Writing plugins for Wordpress may seem like a daunting task if you're new to Wordpress. But all it takes is a good starting point to get the ball rolling. Today I'll show you how to get started with your first simple Wordpress plugin. For this example, we will be making a plugin that automatically adds an image to all new posts.

First, you will need to make the appropriate directories and files. In your /wp-content/plugins/ directory, make a new folder with your desired plugin name, and a .php file with the same name.

You should have a structure similar to this: /wp-content/plugins/auto-image/auto-image.php making sure that whatever name you chose, you use it for both the file and directory.

Open up the .php with your favorite editor and add some basic plugin info into the file header:

<?php

/**

 * Plugin Name: Auto Image Plugin

 * Description: Automatically adds an image to every post.

 * Version: 1.0

 * Author: SpinSpire

 * Author URI: http://www.spinspire.com

 */

That's enough for Wordpress to see your plugin, and you can now enable the plugin, although it doesn't do anything yet.

Still in the same .php, add these lines below:

function auto_add_image( $content ) {
  return $content . '<p><img src="YOUR IMG URL HERE"></p>';
}

Hopefully your function name doesn't conflict with existing functions, so give it a somewhat unique name.

Finally, add add_filter('the_content','auto_add_image', 1); between the plugin info and the function start.

This will call a Wordpress function add_filter where the first value passed is the hook name "the_content" and the second being your function name "auto_add_image".

You should have something similar to this:

<?php
/**
 * Plugin Name: Auto Image Plugin
 * Description: Automatically adds an image to every post.
 * Version: 1.0
 * Author: SpinSpire
 * Author URI: http://www.spinspire.com
 */
 
add_filter('the_content','auto_add_image', 1);
 
function auto_add_image( $content ) {
  return $content . '<p><img src="YOUR IMG URL HERE"></p>';
}

Now when you replace the placement text with your image URL, all you have left is to save and then enable the plugin under Wordpress Plugins settings.

Obviously this is a very primitive plugin, but it shows how simple it is to get started with Wordpress Plugins.

Make sure to read up on http://developer.wordpress.org/ and the Wordpress Codex for more documentation on Wordpress and writing plugins for it.


tags: Knowledge Base wordpress plugin development