Posts: 661
Threads: 52
Joined: Feb 2011
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.
Posts: 6,266
Threads: 181
Joined: Sep 2011
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.
Posts: 2,094
Threads: 54
Joined: Jan 2011
2012-09-24, 22:14:46
(This post was last modified: 2012-09-24, 22:16:54 by nime.)
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
Posts: 661
Threads: 52
Joined: Feb 2011
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
Posts: 661
Threads: 52
Joined: Feb 2011
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.
Posts: 2,094
Threads: 54
Joined: Jan 2011
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.
Posts: 6,266
Threads: 181
Joined: Sep 2011
mikeh Wrote:The problem with your second suggestion is there will be re-declared errors
Thats what require_once is for.