One of the core aspects of the WordPress platform that we all know and love, is it’s emphasis on freedom and transparency, and an open dialog between platform, developer, and end user. With these freedoms comes the opportunity to develop software in whichever style one chooses, with any customized interfaces or stylings.
With great power comes great responsibility. Over time, overly-complex interfaces and code structures can create high maintenance, as well as a not so extensible, structures for other developers to integrate with. Today, I’ll be running through a few core concepts to apply when creating your WordPress plugins, to ensure you are creating an extensible, maintainable, and comfortable environment for your end user, other developers, and for yourself.
Plan Your Plugin and Know Its Purpose
“A stitch in time saves nine,” as they say. Spend some time planning out the feature set of your plugin. If you can distill your plugin’s purpose to a single sentence, you’re ready to start coding.
An example of this can be seen in WooCommerce (“sell anything online, beautifully”) and in Gravity Forms (“easily create online forms for your website”).
Having a clear purpose for your plugin provides clarity and vision going forward with development.
Use the User Interface Guidelines Provided to You
Your plugin may well be the “next big thing” or “a game changer.” But it’s still powered by WordPress. Developing your own customized interface and disregarding the WordPress interface guidelines is doing your users a dis-service, while also making a stack more work for yourself.
Your users are familiar with WordPress, so save them some learning time and develop using the WordPress interfaces. Your users will be familiar with this style and, as a result, will quickly come to understand how to make use of your plugin.
Be Mindful of Any Setup Tasks Required
If your plugin requires anything to be setup before your end user can start using the plugin, make sure to let them know of this. I’ve found a sustainable approach for this can be using admin notices within WordPress, displayed only if the required fields are empty.
Common use cases for this are API keys or if a specific username or user-selected setting is required.
A template for this kind of message could be: “[plugin_name] is almost ready. To get started, set up your API keys.”
The “set up your API keys” text would then link to your settings screen.
Make Logical Use of Action and Filter Hooks
Your plugin sits inside a WordPress installation and sits next to many other plugins, developed by anyone in the world. For your plugin to truly be able to gain worldwide traction and adoption by other developers, it’s critical to enable developers to hook in, filter, and customize your plugin via their own code. This is done by adding logical placement of do_action() and apply_filters() calls.
If you have an action which forms part of your processing (for example, changing the status of an order in WooCommerce, or displaying your orders table to the customer), place logical calls to do_action() in those functions, to enable developers to add their own code to these processes and outputs.
One Method, One Purpose
Keep your code lean! Each method you develop should serve a single purpose and nothing else. Sticking to this principle forces one to simplify each piece of the code, making it easier for developers to extend, while also making debugging of the code a breeze.
There are also opportunities here for interesting uses of do_action(). Having your methods output simply with a do_action() call (for example, for frontend output), enables themes to make use of this action without any unnecessary conditional statements.
An example of this can be seen in the “Testimonials by WooThemes” plugin. Calling do_action( ‘woothemes_testimonials’ ); will do nothing if the plugin isn’t present, yet doesn’t throw any errors either. If the plugin is present, the testimonials will display. That’s a clean, single-line method of displaying testimonials without having to write a conditional statement in the theme’s code.
Keep It Simple, Stan
Ultimately, the greatest asset to any plugin is simplicity. Keep your plugin’s purpose clear and simple, while also offering a clean and familiar interface for your end users. However advanced the logic is behind the interface, keeping the interface simple will ensure peace of mind for yourself and your end users.
Think like an end user and consider their needs from your plugin. This approach will help with forming the best possible plugin to meet your end users’ needs.
2 Comments