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"

  1. randypeterman.com/wordpress/ » Writing a WordPress Plugin: Part II July 23rd, 2004 at 2:46

    [...] 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 [...]

  2. wordlog.com » How to write a plugin July 25th, 2004 at 5:31

    [...] 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 [...]

  3. hitormiss.org: Writing a WordPress Plugin July 28th, 2004 at 2:45

    [...] HitOrMiss.Org Archives About Links Projects Contact

    Writing a WordPress Plugin

    Permalink

    Comments RSS feed fo [...]

  4. rich boakes September 24th, 2004 at 13:55

    [...] 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 [...]

  5. davidhay » Blog Archive » Wordpress As Bookmarks Folder December 5th, 2004 at 21:51

    [...] I do want a crack, at least there are guides available on writing plugins for Firefox and Wordpress. Th [...]

  6. rich boakes September 24th, 2004 at 7:55

    extending stattraq
    a wordpress plugin that adds a view counter to the metadata available with every post – requires stattraq.

  7. The Pordcast » One Thousand July 12th, 2005 at 16:37

    [...] 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 [...]

  8. AegisRyan’s Island » temporary..own purpose September 10th, 2005 at 0:43

    [...] Writing WordPress Plugin Part 1: link… skyfish @ 2:39 pm Posted in: development [...]

  9. ?????Geek?? » Plugin API November 30th, 2005 at 7:23

    [...] 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. [...]

  10. CodeProfessor » Create Your Own Wordpress Themes + Plugins June 30th, 2006 at 21:53

    [...] 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. [...]

  11. Jenny August 16th, 2006 at 10:58

    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

  12. Wild » Blog Archive » WordPress Plugin API September 14th, 2006 at 21:37

    [...] 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. [...]

  13. Das WepLock » Blog Archive » del.icio.us vom 20.02.2007 February 21st, 2007 at 3:59

    [...] The Pordcast » Writing a WordPress Plugin:Part ITags: wordpress plugin tutorials [...]

  14. Scrivere un plugin per Wordpress September 5th, 2007 at 0:34

    [...] ultimo segnaliamo anche questa pagina. Tags del post: Blog, Plugin Wordpress, Wordpress Condividi questo [...]


Leave a reply