Writing WordPress Widget Best Practices
Don’t Nest Functions
It appears to be a neat trick, but that is all it is. A hack that you should avoid and just because you can do it and it works does not give reason to do so. There are many programming practices that you can use in PHP, but must not. The only exception was, if you were the only one that is going to see it and it was an exercise of academics. Aside from that, the common widget practice of nesting functions inside of functions should be halted.
Example of Wrong Widget Form
function myplugin_widget_init()
{
function myplugin_widget_display($args)
{
}
function myplugin_widget_control()
{
}
// Register myplugin_widget_display() and myplugin_widget_control()
}
The problems with this code are listed below.
- Class structure would be the most logical alternative.
The simple containment of the functions is easily accomplished using a class container. I’m unsure what the appeal of this form, but if the need to contain the three functions, than a class should have been used instead.
I have an theory, that the first one to use this pattern knew what he/she was doing and thought it would be cool and didn’t realize that everyone else would use it as an example. Everyone else that came after noticed that it worked (mysteriously) and decided that it was easier than learning OOP. However, this theory is unproven, so whatever the reason, a class would have been a better and wiser choice.
- Prevents Opcode Caching optimization.
PHP will not compile the two functions contained in
myplugin_widget_init()until that function is called. By halting the compile process, some one using an Opcode Cache will not receive the boost. Furthermore, PHP will not know in most cases whether it will need to continue to cache the two functions, so at the best case the functions will be cached after the function is called and at the worst case, it will have to compile the two functions with each process. - Causes fatal duplicated function definitions.
In the event that
myplugin_widget_init()is called twice, it will cause PHP to try to compile the contained functions twice. This will fail, because PHP already compiled the functions when the function was called the first time. The reason you won’t have a third call is that the second call will bring PHP and WordPress crashing down.
Widget Best Practice: Using the Object Pattern
class myplugin_widget
{
function init()
{
// Register myplugin_widget::display() and myplugin_widget::control()
}
function display()
{
// Echo content
}
function control()
{
// Echo administration control
}
}
I like keeping functionality contained together. Some plugins choose to dump all functionality into the same object, I would advise against this, but if it is easier for you, then just use functions instead until you learn OOP better.
Widget Best Practice: Using Functions
function myplugin_widget_init()
{
// Register myplugin_widget::display() and myplugin_widget::control()
}
function myplugin_widget_display()
{
// Echo content
}
function myplugin_widget_control()
{
// Echo administration control
}
This code is far nicer looking and contained and has none of the disadvantages listed above.
February 4, 2008 Posted by Jacob Santos | Writing Plugins Series | best practice, widgets, WordPress | 1 Comment
About
Hello, my name is Jacob Santos. I live in Springfield, MO USA.
All content is licensed under GPL v2. No rights reserved.
Categories
Top Posts
Tags
acceptance testing api automated testing codex comments contributing to wordpress diff Documentation effort functional testing hooks informal wordpress roadmap inline documentation kses maintainability new feature ohloh patch patching php phpdoc planning plugin plugin api plugins plugin standards procedural quality assurance regression testing reporting bugs review roadmap singleton stale patch subversion suggestions test cases tutorial unit testing WordPress wordpress 2.5 wordpress codex wordpress documentation wordpress testing wp-includes
-
Recent Posts
Meta
Blogroll
Archives