Writting your first WordPress plugin
Filed under How-to, Programming, php, wordpress.
Viewed 6184 times times.
Wordpress is a highly used and easily customizable platform for content creation and management. In this post I’ll guide you through the basic structure and steps required to create a simple and useful wordpress plugin.
The first step is to define the plugin header that wordpres uses to determine which files it should include in the plugin list:
1 2 3 4 5 6 7 8 9 | <?php /* Plugin Name: wp-Octave Plugin URI: http://www.bgoncalves.com/notes/2007/05/21/wp-octave-bringing-the-power-of-octaves-plotting-capabilities-to-wordpress/ Description: Easily include Octave's output in WordPress posts. Version: 1.0 Author: Bruno Goncalves Author URI: http://www.bgoncalves.com/ */ |
Here we define the plugin name, version and a brief decription as well as some information about the author and the URI where the latest version can be downloaded.
Wordpress runs each post contents through various filters before actually displaying the end result to the user, with each filter modifying the content in some way. Most plugins simply declare new filters that allow them to add all sorts of functionality to wp. This is done with a line of the form:
174 | add_filter('the_content','octaveFilter',1); |
that simply tells wordpress to filter “the_content” through the function “octaveFilter”. The number “1″ tells wp that this plugin wants to have priority 1, that it wants to run before any other plugins.
In this simple example, the function “octaveFilter” will just go through the content, pick out the parts in between consecutive “[octave]”, “[/octave]” tags and send them to a new function to be processed. A new version of the content is then created where those pieces were replaced by the some useful html code. The filtered content is then returned back to wordpress. A basic implementation of this function would be:
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | function octaveFilter($content) { $start=0; $newcontent=$content; while(($start=strpos(strtolower($content), '[octave]',$start))!==false) { $end=strpos(strtolower($content),'[/octave]',$start); if($end===false) break; $start=$start+8; $data=substr ($content,$start, $end-$start); $result=octaveRun($data); $newcontent=str_replace('[octave]'.$data.'[/octave]',$result,$newcontent); $start=$end+9; } return $newcontent; } |
where we just use simple string functions to pick out the pieces we are interested in. Much more functionality (and complexity) could be added by using Regular Expressions to perform the matchings.
Now that we have the basic functionality of our plugin implemented (with the gory details hiding inside “octaveRun”) it’s time to make it more user friendly by adding an options page, using:
181 | add_action('admin_menu', 'octaveGetOptionsPage'); |
where we tell wordpress to call the “octaveGetOptionsPage” function whenever a user goes to the administration menu. In turn, this function:
35 36 37 | function octaveGetOptionsPage() { add_options_page('wp-Octave', 'wp-Octave', 8, __FILE__, 'octaveOptionsPage'); } |
explains to wordpress where to find the function that implements the options page functionality, that it should only be visible to users of level 8 or above and that the name of the new options pane is “wp-Octave”. The function “octaveOptionsPage” is responsible for outputting the options form and updating the options according to the user input using wordpress’s built in options “API”. This “API” has two basic functions, “update_option” that updates the value stored in wordpress db (or adds it if it doesn’t exist already) and “get_option” that just retrieves the value stored. The syntax for “update_option” is a simple as it gets:
45 | update_option('octaveBin',$_POST["octaveBin"]); |
it just assigns the value received by the form to an option identified by the string in the first argument.
Unsurprisingly, the syntax for “get_option ” is even simpler, requiring just the option’s name:
11 | $octaveBin=get_option('octaveBin'); |
With just this rudimentary understanding of the wordpress plugin API and some knowledge of php, you should now be able to contribute a useful plugin to wordpress’s community. You can download the complete (and fully functional) plugin file below by just right clicking the link and please leave any questions or suggestions you might have in the comments area.

Blog Index
Subscribe via Email

May 25th, 2007 at 1:48 pm
[…] man auf Bruno Goncalves - Writting your first WordPress plugin ein (sehr) kleines Tutorial, aber für den Anfang reicht es. Posted by Martin on Freitag, Mai […]
May 28th, 2007 at 11:04 pm
[…] Bruno 的一份 WordPress Plugin 入门教程。 With just this rudimentary understanding of the WordPress plugin API and some knowledge of php, […]
May 29th, 2007 at 9:07 am
can’t download the attached files
May 29th, 2007 at 11:44 am
lzyy,
Problem solved. I think you should be able to download it now without any problems.
Thank you for the heads up!
May 31st, 2007 at 1:14 pm
[…] the Zend Developer Zone points out, there’s a great introductory tutorial that anyone should check out if they’re looking to get into the world of programming plugins […]
June 15th, 2007 at 4:41 am
[…] harici kaynak yazılar işi daha iyi anlatmışlardı.Bunlardan biri de Bruno Goncalves’in Writting Your First WordPress Plugin başlıklı yazısı olup, bu yazımda onun yazısının, farklı bir örnek üzerinden çevirisini […]
July 10th, 2007 at 11:37 am
[…] writing your first WordPress plugin : simple example and how to use Hooks , filters […]
September 7th, 2007 at 10:07 pm
Hi
great job!!!
G’night
September 8th, 2007 at 4:00 pm
This is my first post
just saying HI
September 14th, 2007 at 10:32 pm
[…] 原文地址:click here […]
September 21st, 2007 at 3:21 pm
Hello
Great .Now i can say thank you!
December 15th, 2007 at 8:56 am
very interesting, but I don’t agree with you
Idetrorce