Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Great question! –  MikeSchinkel Aug 14 '10 at 21:45
    
"Is the only real solution to ask the author to include custom hooks so that other developers can build off of their work?" In most cases, yes. There's certain situations where you can manage to hook into their plugin indirectly, but it's not usually the case. Tell them to write their plugins as if they were writing for the WordPress core -- include a filter/action wherever one might need it. –  Viper007Bond Aug 15 '10 at 1:50
1  
Hint: Document the hooks your plugin does provide so others plugin authors can interact with it. –  hakre Aug 17 '10 at 11:37
add comment

4 Answers 4

up vote 6 down vote accepted

@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.

share|improve this answer
add comment

Do we just need to try and to be better plugin developers so that we can all play nice together?

For starters, yes.

Is the only real solution to ask the author to include custom hooks so that other developers can build off of their work?

That would be a good solution.

You could also copy the other plugin and just add any changes, though that would be more hassle.

share|improve this answer
add comment

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:

Do we just need to try and be better plugin developers so that we can all play nice together?

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?

share|improve this answer
    
That would work, but what if a different plugin's function had the same name? –  Arlen Beiler Aug 14 '10 at 19:59
    
Oops, maybe it wouldn't, because you have to know what is being retweeted and by whom. Checking for the function is a run-time solution, what we need is a real-time solution, one that runs when the function in question is called or returns data. –  Arlen Beiler Aug 14 '10 at 20:01
    
@Arlen - Say you were to create your own function which checks whether the particular function is there. It would be run after 'plugins_loaded', and if it is there it sets a boolean which enables the features. It is highly dependent on the code not changing, but I can't see why it wouldn't work? –  Thomas McDonald Aug 14 '10 at 21:03
add comment

define a function:

function my_footer() {
    do_action('my_footer');
}

now you can hook on this hook: add_action('my_footer', 'example_function', 1);# more informations see on my post.

share|improve this answer
    
not => now, this own => this. Correct? –  Arlen Beiler Aug 14 '10 at 19:40
    
I understand how to add custom hooks - the question is how to hook off of someone else's plugin, ideally in a way that doesn't require direct modification of their plugin. –  Ryan Elkins Aug 14 '10 at 19:41
    
@Arlen: many thanks, sorry for my bad english @Ryan: it is not possible, when dont have the plugin a hook; but you can check for replace the function or class, when the author ask for this in his plugin. But it is possible to ask for active plugins and hook earlier in WP as the other plugin and replace his function. –  bueltge Aug 14 '10 at 21:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.