Writing a WordPress Plugin:Part I
- Monday Jul 5,2004 08:51 PM
- By Randy Peterman
- In News
I’ve been working on ‘StatTraq‘ a WordPress statistics plugin and have been able to find reasonably good documentation and have supplemented the rest of my needs for<> information with ‘Find in Files’, a useful feature of TopStyle. I’ll share what I’ve learned so far in a series of articles on programming WordPress 1.2 plugins.
The first thing you’ll need in creating a WordPress plugin is a set of hooks for either actions or filters. These hooks are places in the code that call your functions when an event or certain text is processed. I’ll explain both in further detail below. A list of actions and filter events can be found at the WordPress Wiki.
Actions
Actions are events that take place during the WordPress document creation cycle. When WordPress gets a request from a client (browsers like Firefox, RSS aggregators like FeedDemon or any other device like a WAP 2.o/XHTML basic phone) and the PHP application starts up. WordPress gives you the ability to call a function or functions when certain events take place.
For example with my statistics plugin I need to add information about the request to the database, to make sure that I didn’t hender the the display of the WordPress data I added a hook for the ’shutdown’ action like so:
add_action('shutdown', 'stat_traq_event');
This allows the stat_traq_event function that I created to initiate the data entry into the database to be called after the page is created but before PHP shuts down.
Filters
The key to your plugin is determining what event should trigger your plugin. Some plugins are filters, which means that they should be run when certain text is output. For a plugin that turns your article text into 1611 Old English (the year the King James Bible was published) you’d want your filter to be called on whenever text was output.
add_filter('the_content','thine_olde_English_plugin');
When ‘the_content’ is requested the old English plugin will be passed the text of the article to manipulate it. In this case words like ‘your’ would be changed to ‘thine’ and ‘you’ would be changed to ‘thee’ and ‘donkey’ would be changed to ‘ass.’
Be sure to check out the API documentation on the wiki and feel free to participate on the WordPress forums or post questions here [as a comment], if I know the answer I’ll write you, if I don’t I’ll see if I can send you a link to somewhere with the answer.
In Part II I’ve written about interacting with the database in WordPress, something that is easy and comes with a few nifty tools built in.
14 Responses for "Writing a WordPress Plugin:Part I"
[...] lugin: Part II
Filed under: Programming — Randy Peterman @ 8:42 pm
In Part One we discussed adding actions and filters to WordPress as an initial step in writin [...]
[...] Randy Peterman has written a two-part article on how to write wordpress plugins. Part 1 Part 2 He is the author of Stattraq a plugin + hack to analyze the visit statis [...]
[...] HitOrMiss.Org Archives About Links Projects Contact
Writing a WordPress Plugin
Permalink
Comments RSS feed fo [...]
[...] stattraq author randy has done (aprt from writing stattraq) is write up a few words about hacking wordpress. and as a kind of review of these notes, i had a go at knocking up a wo [...]
[...] I do want a crack, at least there are guides available on writing plugins for Firefox and Wordpress. Th [...]
extending stattraq
a wordpress plugin that adds a view counter to the metadata available with every post – requires stattraq.
[...] u endure this blog but don’t like any of my writing but one piece)? I’m proud of some of the things that people search for on the ‘net and get to my site for, and [...]
[...] Writing WordPress Plugin Part 1: link… skyfish @ 2:39 pm Posted in: development [...]
[...] Hello Dolly is WordPress’ first plugin. The commented source for that plugin might help you understand how plugins work and act as a guideline for the structure and syntax to use in writing your own plugins. There is also an introductory article (http://www.randypeterman.com/wordpress/index.php?p=597) Part II (http://www.randypeterman.com/wordpress/index.php?p=648) that may prove useful. A simple explanation of the database tables can be found on the Database Description page. [...]
[...] Randy Peterman’s Pordcast also has some tips in Writing a WordPress Plugin – Part I and Part II. However, this is aimed at WordPress 1.2, so you may need to use this with caution. A more recent tutorial, Customizing WordPress Plugins, is available at Dev Lounge. [...]
The commented source for that plugin might help you understand how plugins work and act as a guideline for the structure and syntax to use in writing your own plugin
[...] Hello Dolly is WordPress’ first plugin, and the commented source on that page might help you understand how plugins work, and act as a guideline for the structure and syntax to use while writing your own plugins. There is also an introductory article [Part II] that may prove useful. A simple explanation of the database tables can be found on the Database description page. [...]
[...] The Pordcast » Writing a WordPress Plugin:Part ITags: wordpress plugin tutorials [...]
[...] ultimo segnaliamo anche questa pagina. Tags del post: Blog, Plugin Wordpress, Wordpress Condividi questo [...]
Leave a reply