Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to allow one plugin to use anothers functions
#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


Messages In This Thread
Best way to allow one plugin to use anothers functions - by mvlcek - 2012-09-24, 22:14:46



Users browsing this thread: 1 Guest(s)