How do I revise a theme so that I can publish my event hooks and anyone can build a plugin to add new functionality easily to my theme?
After working on several projects so big I didn’t even know all the people involved, I came to one conclusion:
Keep it simple, write great documentation.
Code is simple if it is easy to read, to learn and to extend.
Do not reinvent the wheel: Use the given hooks wherever possible, add new ones in a predictable scheme.
A very basic example:
By looking at the
Register all callbacks for your hooks in one place: the start of the
Include additional files as late as possible, make it easy to replace those files, and use one file per class.
Use PHPDoc for all functions and classes, add the hook each one is called.
Avoid code that makes plugin code hard to write:
And last but not least: Use version control (Git, Mercurial), write atomic commits, and explain in each commit message why you made this change.
Try to use standardized hooks. Check out the ones from the Theme Hook Alliance: https://github.com/zamoose/themehookalliance
There really is no definite answer to your question: How to make a theme "plugin-ready"?
Although, there are several things which you should make use of in your theme. I cannot list every single thing you should do in such great detail. However, I can provide a short list with a quick explanation why you should be using them.
WordPress Core Functions
If you are ever unsure of a particular function, and you know it has to exist somewhere, try visiting Query Posts, for a better WordPress code reference.
WordPress Hooks (Actions and Filters)
The next thing you should consider would be WordPress Hooks, or... Actions and Filters to be politically correct.
By default, WordPress already provides support for many events triggered in your theme if you're using those functions posted in the numbered list, above this section.
These "events" are referred to as Hooks, which allow plugin developers to add, modify, or remove code from certain areas within your theme. Or fire a certain event, when another event is triggered in your theme.
Which brings us to the next area you should be thinking about, while creating a "Plugin-Ready" WordPress theme.
Creating your own WordPress Hooks
It would be wise to learn how to create your own WordPress actions and WordPress filters, within your theme. This will allow plugin developers to HIGHLY manipulate your WordPress theme.
If you don't know how to create WordPress actions or WordPress filters, click here to learn more about creating WordPress actions with
Simply just by using more of the default functions provided by WordPress core (Like the ones listed in the first numbered list at the beginning of this answer, or else here for a full function index), a LOT of the hooks that developers would need to use, are already defined by the core.
Do not ever be shy with creating your own WordPress hooks, within your WordPress theme. It's always better to have many many hooks available for developers to tap into and make use of, rather than not enough to do what they need to do.
Just remember to use unique references to your hook names. (They must be unique, so that they do not conflict with either existing WordPress Core hooks, or existing WordPress hooks created by other plugin developers.)
By creating your own hooks, WordPress plugin developers can hook into your custom created WordPress hooks using
Which is great, when you release an update for your theme, because their changes to your theme will be persistent and won't be over-written or lost by the theme update.
As an alternative to your answer why don't you do something similar to this instead?
In your theme file place a hook,
1) Separate every theme file so that you have PHP at the top, and HTML at the bottom. Then, in the HTML portion, inject variables in like so:
or use PHP Alternative Syntax stuff minimally for loops and if/then/else conditions. Keyword -- minimally. See, the bulk of your PHP should be at the top of the file.
You might be scratching your head on why the
See how easy it is to debug? Plus, because I use ALL CAPS on the variable name, I can find them easy in the HTML portion of the page. And why did I call it
2) Move any library or class file includes to the top of the PHP file such as:
3) Following the library includes, set any constants or static pieces of information, or read from
4) Following the "settings" initializing area (step 3), do any
5) Following the GET/POST variable processing (step 4), add this snippet of code, and note that I used "mytheme_" as a prefix for the theme name, but you can change "mytheme_" to something else.
6) Right before you do the
7) At the end of the file, after all the HTML, add this snippet of code:
Now that this is done, you can code a fresh plugin and when you use
You can use
Don't forget to do this even on the admin panels you create with your theme, not just the frontend of the theme.
Note on the several lines above you can reduce this a little with a couple functions, such as one for finding the hook name. Just don't put the line that has
|show 6 more comments|