Writing a WordPress Plugin:Part I

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.

16 thoughts on “Writing a WordPress Plugin:Part I”

  1. Pingback: rich boakes
  2. Pingback: rich boakes
  3. 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

  4. I m developping a wordpress plugin and i m new in wordpress so i create my formular but when i write doesn’t work in wordpress so i search solution from the wordpress site and i find that i have to use register_setting, get_options, update_options and more but i this fonction save data in wordpress table “wp_options” but i want to get input and save it in my own table that i create in wordpress db

    pleaze help me

    thank for all

Comments are closed.