Plan Your Plugin Before You Code
It’s amazing to think that you can create a custom wordpress plugin with the simple knowledge of code.. ie php, css, html and js. This post we will go over how to plan your custom wordpress plugin development.
Believe it or not, code is a bit like golf. Winning means you did as little of it as possible, in the fewest strokes.
In the same way that a professional golfer spends a lot of time strategically measuring the elements before ever swinging their club, you’ve got to plan your approach to code before you proverbially step foot on the course.
If you plan it well, actually coding the plugin should be where you spend the least amount of time. Hypothetically.
Start On Paper
Outline the detailed functionality of your plugin.
What does it do? What features does it need? Which users can add or delete things? Which users can see them at all?
Think through everything you want from your plugin. Consider every element that your plugin is going to need. Write it all down.
Organize the similar bits together.
You’ll wind up with a lot of scattered notes, so now you’ve got to start grouping together the bits that are related to each other.
As you work, think in terms of WordPress. What functionality fits this best? Would it work as a custom post type? A new taxonomy? Is it a widget? Are you making really custom database queries, or can you use <code class=”highlighter-rouge”>get_posts()?</code class=”highlighter-rouge”>
Start developing the logic.
This is the hardest part: you must pretend to code your plugin without actually coding it. Code on paper, if it helps.
The point is learning how to solve problems pragmatically without getting caught up in bug fixing or syntax errors. Put your plugin together piece by piece, thinking through the logic it needs to work. Identify which data you need to to communicate to WordPress, and which data you’ll need to collect from WordPress. How will you do that?
To find the answer, start researching specific WordPress techniques.
If you can’t find what you need, Google specific bits and pieces. You may be surprised what people have already experimented with! Bookmark the tutorials and code snippets that seem relevant to your project.
It’s okay to take some time to work through tutorials as you research, if things get too confusing. It’s all part of the process. Ultimately, you’ll be better informed and come up with stronger solutions.
Plan out your project structure.
Everything is starting to come together, but you should also consider how you’ll organize the actual files for your project.
Here’s how I organize my plugins these days:
/plugin-slug plugin-slug.php /admin # admin settings pages /assets # images, styles, scripts /css /img /js /library # all classes and other files class.example.php /tests # unit tests
Finally, Start Coding
By now, your plugin should have already taken some definitive shape before you even opened the code editor. Remember? I told you I wanted to teach you how to build plugins like the pros, not hack together a makeshift plugin that fails under pressure.
Before you get started on your code, always remember to work using the two following rules.
Don’t Repeat Yourself (DRY)
WordPress has many, many functions that already do a lot of what you need. Not all of it is well documented in the Codex, but it’s in there if you search hard enough.
For example, I started trying to build my own slug sanitization function to enable custom permalinks for a custom post type I was building, when I realized WordPress must have one of its own. About 15 minutes later, and I was happily using the built-in function <code class=”highlighter-rouge”>sanitize_title()</code class=”highlighter-rouge”>.
Before spending a lot of time on complicated custom functions, triple check whether it’s out there already.
Build on the framework, not next to it.
As stated before, WordPress is packed with amazingly powerful and flexible tools.
To create a good plugin, you should always work within the framework as much as possible. It can make the difference between paddling hard up stream all day to reach your bare minimum goals, and being able to start your own logging company by floating easy with the current and being able to focus on the quality of your lumber instead of whether the ship will sink. (Sorry, another random comparison, I know.)
The moral of the story is, let the easy things be easy.
Use custom post types, taxonomies, meta data, and all the other goodies of WordPress to your advantage. Focus your effort on what makes your plugin clever and unique.
More Plugin Development Resources
If you’re looking for even more reading, tutorials or content, here are a few more resources the check out: