Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to allow one plugin to use anothers functions
#1
I am wondering what the best method is to allow all installed plugins use a function from one plugin.
For my Multi User plugin I have 3 plugins that other plugins can use:
  • add_mu_permission()
  • check_user_permission()/*]
  • check_user_permissions()

Now this worked fine when my blog plugin used add_mu_permission() to add permissions and the other two to retrieve them. However, I have another plugin of mine, named "lead_management" in which the mentioned functions do not work. They seem be not defined yet. If I try to check to see if they are undefined and define them, the multi user plugin would throw an error as it would be trying to re-declare the function.
Reply
#2
You can either , not use any of them until after the "common" hook is called ( After plugins are loaded ).

Or check for functions exist and if not include the plugin file yourself.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
mikeh Wrote:I am wondering what the best method is to allow all installed plugins use a function from one plugin.
For my Multi User plugin I have 3 plugins that other plugins can use:
  • add_mu_permission()
  • check_user_permission()/*]
  • check_user_permissions()

Now this worked fine when my blog plugin used add_mu_permission() to add permissions and the other two to retrieve them. However, I have another plugin of mine, named "lead_management" in which the mentioned functions do not work. They seem be not defined yet. If I try to check to see if they are undefined and define them, the multi user plugin would throw an error as it would be trying to re-declare the function.

First, all the functions in your plugins should use a plugin specific prefix/code, like add_mu_permission, check_mu_user_permission, check_mu_user_permissions.

Then any other plugin can use the functions with
Code:
if (function_exists('check_mu_user_permission')) check_mu_user_permission();

You can also encapsulate this call in a plugin specific function (with its own prefix/code) and use this function in your plugin (xyz = code for this plugin):
Code:
function check_xyz_user_permission() {
  if (function_exists('check_mu_user_permission')) { // Multi user plugin
    check_mu_user_permission();
  } else if (function_exists('check_emu_user_permission')) { // extended multi user plugin
    ...
  } else ...;
}
(better even to put all your non-public plugin functions in a class to minimize names in the global name space)

Just remember that the plugins are loaded in a random order (it depends on operating system, file system, etc.) thus
  • do not call any other plugin's functions in the plugin initialization (outside any trigger or filter), as the other plugin might not be loaded yet
  • do not try to redefine functions from other plugins but rather use another unique name, as the plugin might be loaded later on and will throw an error
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#4
shawn_a Wrote:You can either , not use any of them until after the "common" hook is called ( After plugins are loaded ).

Or check for functions exist and if not include the plugin file yourself.
The problem with your second suggestion is there will be re-declared errors
Reply
#5
mvlcek Wrote:First, all the functions in your plugins should use a plugin specific prefix/code, like add_mu_permission, check_mu_user_permission, check_mu_user_permissions.

Then any other plugin can use the functions with
Code:
if (function_exists('check_mu_user_permission')) check_mu_user_permission();

So from my understanding, in the plugin that is using the other plugins functions, I should create a function and put all references to the other plugins functions in it. Then call that function with the common hook.
Reply
#6
mikeh Wrote:So from my understanding, in the plugin that is using the other plugins functions, I should create a function and put all references to the other plugins functions in it. Then call that function with the common hook.

Yes, when you encapsulate the calls to other plugins in your own functions, you can more easily add support for plugins with similar functionality. Of course, if you call this other plugin's function just once, this might just be overhead.

Just don't try to define a function, if it is not defined yet - it won't work consistently.
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply
#7
mikeh Wrote:The problem with your second suggestion is there will be re-declared errors

Thats what require_once is for.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply




Users browsing this thread: 1 Guest(s)