30 July Fri, 2010

How to write a Wordpress plugin: episode 1 – getting started

Wordpress is amazing, let’s get that out of the way. While I don’t use it for this website I do use it for a couple of other things and it’s just an incredible project – but what makes it incredible? It’s the variety and depth of plugins that are available. So I’m going to be writing a series of tutorials on “how to make a Wordpress plugin”. Welcome to episode one.

Wordpress plugins are the single best way of integrating new functionality into a Wordpress install. You write the plugin, upload it to your site and activate it. If you grow tired of it, you de-activate it – it really is that simple. There are hundreds of great plugins already available but it’s always handy to know how to write one yourself, so here we go.

Episode 1: Making a very simple plugin and getting it recognised
The first step is to actually create your plugin and get it recognised within Wordpress before trying to include any functionality. So in this episode we’re going to create a very basic plugin that doesn’t actually do anything and install it.

#1 – Headers are important
Wordpress needs quite a strict setup in the head of the PHP file to identify the attributes of your plugin, so I’ll show you how mine looks:

  1. /*  
  2. Plugin Name: Seopher.com Demonstration Plugin  
  3. Plugin URI: http://www.seopher.com  
  4. Description: This is just a demonstration plugin to show how you build one  
  5. Author: Steven York of Seopher.com  
  6. Version: 0.1  
  7. Author URI: http://www.seopher.com  
  8. */  

“Plugin Name”, “Author” and “Description” are rather self explanatory. “Plugin URI” is a direct link to the project page for this plugin, where users can visit for updates, support etc. The version is a number you wish to assign to the release, so version it however you like. “Author URI” is your website. In this instance the “Author URI” and “Plugin URI” are the same because this is only a demonstration plugin rather than a dedicated project.

#2 – Plugin content
I then wrote a function to initialise the plugin, this is called later to write some content onto the specific plugin configuration page (under “Options”, where you see all the other plugin config pages). All this function does at the moment is return some HTML as a token gesture to show that we’ve actually got something working. The “wrap” div is significant because that is a Wordpress wide constant, placing all your content into a suitably styled container.

  1. function initDemoPlugin()   
  2. {   
  3.        
  4.     ?>   
  5.         <div class=“wrap”>   
  6.             <h2>Seopher.com demonstration plugin</h2>   
  7.             This is just some sample content to show that we’ve got control over the content of this page.  Rawr!   
  8.         </div>   
  9.     <?php   
  10. }   

#3 – Add the plugin to the submenu
This step involves writing a function to add the plugin into the “Options” sub-menu so that when it is activated you have somewhere to visit to configure the settings. See the code below:

  1. function addPluginToSubmenu()    
  2. {   
  3.     add_submenu_page(‘options-general.php’‘Seopher demo plugin page’‘Seopher demo plugin’, 10, __FILE__‘initDemoPlugin’);   
  4. }   
  5.   
  6. add_action(‘admin_menu’‘addPluginToSubmenu’);   

All we’re doing is tying into the “add_submenu_page” function, where we’re passing several parameters. Firstly we’re specifying the parent page (in this case options-general.php) that the sub-menu page will sit under. The second parameter is the page title for the new page (as seen in the browser’s title bar). The third param is what appears in the sub-menu navigation. The next parameter is the priority of this function which can just be left at 10 for now. The final param is the function that is called to populate the page, in this case it’s the function we created in step #2.

Finally we use the Wordpress function “add_action” to call the function we’ve just written, simple!

#4 – Upload the PHP file
So if you compile all the code we’ve shown so far (or download my version here) into a PHP file, you can then upload it to the Wordpress install. Upload the PHP file to “/wp-content/plugins/” and you’re done.

#5 – Activate the plugin!
Log into your Wordpress install and visit the “Plugins” tab and you should see something similar to the picture below:

wordpress plugin

That’s your plugin! Click “activate” on the right hand side and it’s now active!

#6 – Visit the config
Click “Options” and you should see our plugin added to the sub-menu as per the screenshot below:

wordpress plugin

Clicking on that takes you to our plugin configuration page. All this page does is output the return of our initDemoPlugin() function that we wrote in #2. So clicking on “Seopher demo plugin” takes us to this page:

wordpress plugin

So there you have it, 6 steps to creating a dummy Wordpress plugin. In episode two I’ll start going through how we start integrating functionality into the plugin and we can start making something useful. I hope this was useful, check back soon for updates.

  • PHP and MySQL 5

    Beginning PHP 5 and MySQL: From Novice to Professional offers a comprehensive introduction to two of the most popular open source technologies on the planet: the PHP scripting language and the MySQL database server.

    Read More | Postd by Ltm.ming
  • Base AJAX Codes and Examples

    Make your Web pages stand out above the noise with JavaScript and the expert instruction in this much-anticipated update to the bestselling JavaScript Bible.

    Read More | Postd by Ltm.ming
  • Mastering CSS with DW CS3

    Durch Integration von Datenbankanbindung und Skriptsprachen wird Dreamweaver CS 4 zum alltagstauglichen Werkzeug für die Erstellung einfacher Redaktionssysteme.

    Read More | Postd by Ltm.ming
  • Pro JavaScript Techniques

    Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites.

    Read More | Postd by Ltm.ming
  • MySQL 5.0 Certification Study Guide

    This book is a very comprehensive guide to what MySQL offers and can teach you. br / br /Would definitely recommend for anyone needing a great book about MySQL.

    Read More | Postd by Ltm.ming
fashion handbags wow gold Hermes Birkin Bags Knockoff bag air max 2010 chanel handbags cheap golf clubs