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 are events that take place during the WordPress document creation cycle. When WordPress gets a request from a client (browsers like Firefox, RSSaggregators 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:
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.
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.
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.
After we discussed adding actions and filters to WordPress as an initial step in writing a WordPress plugin. These hooks into the WordPress API allow your code to be called by WordPress as certain events and code are generated.
However, your plugin may need to access the WordPress database to use WordPress’ data for outputting various pieces of information. WordPress’
$wpdbobject is perfect for the job and easy to use.
The WordPress database interaction object is created in the WordPress code for you, so all you’ll need to do is import it into your functions that are called by WordPress like so:
global $wpdb, $tableposts;
// other code here to make the world a better place
You’ll also need to import the database name that you’ll need access to because WordPress 1.2 uses variables to hold the database names. The database names are dynamic because you can have multiple WordPress installations and each installation has a different prefix. The list of default tables are found in the wp-settings.php file:
You’ll have to determine what table has the data you’re after. I recommend using PHPMyAdmin to look at the information in your database.
OK, so you’ve got access in your plugin, but now you need data, because access is [only] half the battle (to mutilate G.I. Joe). You can use the following functions to get data from WordPress and to control how the WordPress Database object controls output [found in the wp-includes/wp-db.php file]:
- Returns a string escaped for safety in MySQL queries.
- Turns on WordPress returning MySQL error reporting when errors are encountered.
- Turns off WordPress’ returning MySQL errors when they are encountered.
- Sets cached query results to null
- Used to execute DELETE or UPDATE queries
$wpdb->get_var($SQLQuery, $x, $y)
- Use this to get one value from the database, where $x and $y are specific data values. You can call this function with just the query if one result is expected from the query. I have found no documentation regarding the second and third parameter and no where are they used in the WordPress code for version 1.2.
- Returns one database result row from a query.
- Returns the values from a table column for use as a PHP array.
- Returns the SQL query results in an object that can be accessed by the result column names. If you don’t care what object is used for access you can skip the second parameter.
- Returns column meta information for the last column requested. $informationType defaults to ‘name’ and $columnOffset defaults to -1.
Remember that the data returned by the