How to write WordPress Plugins

How to write 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 are events that take place during the WordPress document creation cycle. When WordPress gets a request from a client (browsers like FirefoxRSSaggregators 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.

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:

function suckMyDatabasesBrainsOut(){
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:

  • $tableposts
  • $tableusers
  • $tablesettings
  • $tablecategories
  • $tablepost2cat
  • $tablecomments
  • $tablelinks
  • $tablelinkcategories
  • $tableoptions
  • $tableoptiontypes
  • $tableoptionvalues
  • $tableoptiongroups
  • $tableoptiongroup_options
  • $tablepostmeta

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.
$wpdb->get_results($SQLQuery, $objectForValueToBeReturnedIn)
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.
$wpdb->get_col_info($informationType, $columnOffset)
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 $wpdb->get_results($SQLQuery)

Resources :


10 Responses to How to write WordPress Plugins

  1. […] How to write WordPress Plugins « Developer a few seconds ago from web […]

  2. shreyas says:

    i want to write a function in my plug in
    that should be called in page.php
    but it showing call to undefined function what should i do?

  3. Juegos says:

    Thanks but sounds complicated for a newbie like me.

  4. Traffic guy says:

    In relation to WordPress and WordPress plugins. We have created a really interesting viral traffic plugin that can for sure assist this site increase the traffic. It makes use of the Facebook Like and Share buttons but in a way that you could not imagine. Our plug-in gently pushes it to the users of the website so that they often Like or Share your page much more often than with any other method. You will still be able to still use the normal Facebook buttons by your blogs). This method will allow users for example recommend your main page to all their friends which a portion of will visit and virally increase traffic to your blog. You can adjust the plugin in alot of ways so it suits you. If you visit the site, you can realize how it works. So if you want more visits and viral increase in traffic, come and see for your self. The affiliate program is also very good so you should also look at that even if you think the plugin is not fitting you. Thanks

  5. Don’t let the fear of not knowing limit your ability to make a substantial living online hold you back! This product alone will help you get solid results faster than doing and figuring out all the fussy work by youself!

  6. sean kaye affiliate masterclass…

    […]How to write WordPress Plugins « Developer[…]…

  7. I just wanted to post a quick message so as to express gratitude to you for
    all the lovely techniques you are giving out at this site.
    My incredibly long internet search has at the end been recognized with good quality
    suggestions to exchange with my relatives. I would repeat that
    many of us site visitors are truly lucky to live in a fine website with very many marvellous individuals with valuable opinions.
    I feel very much lucky to have come across the web page and look forward to so many more amazing minutes reading here.

    Thank you once more for everything.

  8. Chante says:

    obviously like your website but you need to test the
    spelling on quite a few of your posts. A number of them are rife
    with spelling problems and I to find it very troublesome to inform the
    reality nevertheless I’ll certainly come back again.

  9. you are really a just right webmaster. The site loading pace is amazing.
    It sort of feels that you’re doing any unique trick.
    Furthermore, The contents are masterpiece. you’ve performed a great activity on this

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: