• Follow me on Twitter
  • Follow my Flickr stream
  • Follow me on Gatorpeeps
  • Add me to your Technorati favourites
  • Follow me on Zoopy

A few guidelines for Wordpress plugin development

The Wordpress plu­gin API is vast and power­ful. It allows developers to essen­tially hook code into almost any area of the Wordpress sys­tem without modi­fy­ing the core files at all. It also allows for the cre­ation of stan­dalone plu­gins that work within the Wordpress sys­tem but do not hook into the core modules.

Over the last few weeks, Wordpress plu­gin devel­op­ment has become one of my favour­ite things to do. I find it excit­ing to be able to cre­ate func­tion­al­ity, incor­por­ate it seem­lessly into the Wordpress sys­tem and see it work smoothly with the other mod­ules. While plu­gin devel­op­ment for Wordpress is incred­ibly power­ful, it also car­ries with it a few areas where people com­monly stumble over and poten­tially lose interest in their code… which could be the next big thing. Here are a few guidelines I’ve picked up in order to step over the stum­bling blocks.

1. Understand actions, fil­ters and hooks, where to use which action and their limitations.

Actions, fil­ters and hooks are what allow developers to insert their code into vir­tu­ally any part of the Wordpress sys­tem. While there are only almost 400 hooks doc­u­mented, there are in fact around 800 hooks in the Wordpress sys­tem. The Wordpress codex has a great ref­er­ence for actions, fil­ters, hooks and how to use them.

2. register_activation_hook() does not func­tion the same way as add_action().

While look­ing through the Wordpress for­ums, the register_activation_hook() func­tion (runs the func­tion you pass to it when the plu­gin is activ­ated) has come up as a reg­u­lar topic of con­ver­sa­tion. Users are attempt­ing to dis­play text on activ­a­tion, using it as if it were the add_​action() func­tion. register_​activation_​hook() does not func­tion the same way. The func­tion, the way I under­stand it, works in an almost over­all scope, not hav­ing access to vari­ables within your plu­gin for dis­play pur­poses. In short, don’t try to echo or print vari­ables with this func­tion. It won’t work. The func­tion is best used for doing com­mon setup such as installing data­base tables, set­ting default options, etc.

3. Use dbDelta instead of mysql_query or $wpdb->query() when installing data­base tables.

The dbDelta func­tion (which you pass your data­base query to) is used when installing tables into your Wordpress data­base. It is also used as an upgrade script in the same area. Don’t for­get to include the Wordpress upgrade script before run­ning dbDelta(). The include looks like this:

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

4. Understand the scope of your plu­gin and work within it.

As men­tioned above, Wordpress plu­gins can be used to hook into essen­tially any area of the Wordpress sys­tem. This includes stan­dalone menus, as well as run­ning actions when a post or page is saved, for example. Understanding the objective/​s and scope of one’s Wordpress plu­gin is essen­tial to choos­ing the cor­rect approach for the plu­gin. If it’s a short plu­gin with spe­cific object­ive, there’s no need, in my opin­ion, for more than one or two files. Keep it simple. On the other hand, if the plu­gin is a large, stan­dalone mod­ule, I don’t believe there’s any reason to have cluttered files of hun­dreds of lines of code, where an Object Oriented approach would ulti­mately serve as a much cleaner and more extend­able solution.

5. Plan, plan, plan.

Planning on paper, draft­ing a code map on screen, talk­ing it through with other developers. Planning a plu­gin can never be done too much. It helps with all of the above steps as well as help­ing to see where you need to be at the end of the devel­op­ment and plan­ning a solid roadmap for the jour­ney to get there. Also, other users may have ideas that could stream­line your plu­gin even further.

These are just a few things I’ve picked up on my Wordpress plu­gin devel­op­ment jour­ney. I’ll post more if and when they come up. I hope these tips are use­ful and help in over­com­ing the poten­tial stum­bling blocks when get­ting star­ted with Wordpress plu­gin development.

Related Posts

Comments
  1. wpar­ena wparena says:

    very use­ful inform­a­tion, thanks for sharing

  2. Matty Matty says:

    Thanks, wpar­ena. I really appre­ci­ate it. :)

    Just checked out your blog and am enjoy­ing brows­ing through your posts. Some very use­ful inform­a­tion there. :)

    Cheers,
    Matt.

Leave a Reply

Designed and Developed by Matty | back to top