I’m working on a plugin that would ideally be able to do things when a user does something through a different plugin.
I can add custom hooks in those plugins but obviously this wouldn’t work well for when I want to distribute my plugin. Is there a good way to add hooks to other people’s plugins from my own plugin? Is the only real solution to ask the author to include custom hooks so that other developers can build off of their work?
Example: I want to do something in my plugin when someone retweets an article. If there was a custom hook in whatever the popular retweet plugin is that I could hook in to and fire off of, that would be great. There isn’t, so I can modify their plugin to include it, but that only works for my copy, and I don’t want to try to redistribute that.
Do we just need to try and be better plugin developers so that we can all play nice together?
@Ryan Elkins:
I guess the answer depends on how import each use-case is to you. In some cases it would be something you need quick-and-dirty, in others it might be a more significant use-case. Here are the two things that come to mind:
Look for Alternate Hooks within WordPress Core
If it’s something quick and dirty some times you can use other downstream hooks from core to modify what you need, or both upstream and downstream hooks via the use of
ob_start()
/ob_end_clean()
(see @Todd Perkins answer to “Dealing with large HTML output via plugin code” for a code example.)To track down the hooks you could use check out the Instrument Hooks plugin I posted yesterday to help you find the hooks that you can potentially use.
Submit a Patch with your Desired Hook to the Plugin Developer
If your use-case is more significant to you or the community I’d recommend going ahead and adding the hook that you need to the plugin. Then test it well to ensure it really addresses your use-case after which you can submit a patch to the plugin developer in hopes that he or she will apply it. This way you make it as easy as possible on them by giving them tested code and you get to work through the use-case yourself to ensure it is really what you need. I can’t tell you how often I thought I needed a certain hook only to find after trying to implement one I needed a hook but one that was different than I first envisioned.
If you are not familiar with creating a patch here is a good article on patching WordPress core for which most applies to patching plugins and for that which does not it will hopefully be obvious what to do:
Hope this helps?
P.S. One thing I find a bit disappointing and that your question addresses is the percentage of plugins that are designed only for end-users, i.e. that have no hooks of their own. Imagine if WordPress were designed like most plugins? It would be inflexible and a very niche solution.
Maybe things would be different if WordPress were to have the ability to auto-install plugins on which other plugins depended? As it is I typically have to write a lot of the functionality I need from scratch because clients want things a certain way and the available plugins, while 90% there don’t allow me the flexibility to update the remaining 10%.
I really do wish those leading the WordPress community would identify a way to ensure that plugins are rewarded for following best practices (such as adding in hooks for other developers) much like good answers are rewarded on a StackExchange site.
For starters, yes.
That would be a good solution.
You could also copy the other plugin and just add any changes, though that would be more hassle.
I think you answer your question in the question, so that it becomes somewhat rhetorical.
Obviously you’re talking about a similar system to the one Google encourages with Android and the Intent system, that an application can publish actions which it is capable of doing on behalf of other applications, which can then hook into them and pass data back and forth. Personally I think it is something which we need to head towards as good developers – we use WordPress because it’s already awesome, awesome enough to make the decision over whether to use it or develop a similar in-house product pretty easy in most cases. The plugin repository itself is again the same thing, mostly as an end user + developer – why develop a twitter plugin when there is a perfectly good one.
The same ‘why develop duplicates’ is at the crux of your question here. The Android Intent system allows applications to utilise already created functions, and pass data between them, and this is popular and often used, because it is pushed so heavily. There is a similar system already implemented into WordPress, but is very rarely used beyond the hooks present in the core code, which are used a great deal.
It would benefit the community if more hooks in custom plugins were present, but as you say, there’s no easy way to add hooks in when/where you need them.
In terms of the Twitter plugin you want to hook into, send the author an email, I’m sure he would be happy to add them in for you.
If there’s a place you think would be a good place to put a hook into in your plugin, do it, and document it well. If more people start to put hooks into plugins, or there is a general push towards doing so, it will happen eventually.
So to answer your ending question of:
Yes.
Edit: I’ve thought some more about the actual question and the best way of implementing hooks, couldn’t you add an action that would run if the particular function you are trying to run of the plugin was present?
define a function:
now you can hook on this hook:
add_action('my_footer', 'example_function', 1);
#more informations see on my post.