WordPress.org

Ready to get started?Download WordPress

Forums

How to call plugin from another plugin ? (3 posts)

  1. mavico
    Member
    Posted 7 years ago #

    I need to call function in plugin from another plugin .. how to do that ?!

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    That's actually trickier than it sounds.

    The obvious way is just to call the function. Simple, sometimes it even works. Few problems with it though:
    a) If one plugin is activated and the other is not, you get an error.
    b) Plugin load order is not guaranteed, so if you are trying to call it at plugin init instead of later from a template call or something, the plugin you're looking for may not have loaded yet.

    You can solve problem A by checking for the function you want before doing anything else.

    Plugin A:
    function call_me(ishmael)
    {
    // do stuff
    }

    Plugin B:
    function caller_func()
    {
    if (!function_exists('call_me')) return;
    call_me(bob);
    }

    This ensures that the function is there, and if it's not, you can take action accordingly.

    Problem B is solved slightly differently. If you need to check for another plugin at initialization, all you have to do is to make your function initialize after all the plugins load.

    function init_my_plugin()
    {
    // do init stuff
    }
    add_action('plugins_loaded','init_my_plugin');

    This way, you can be assured that all plugins are loaded when you do your init routines. And obviously, you'd want the init routine to check for the other plugin's functions before using them too. Some plugins provide action hooks themselves in their init routines, so you can hook into them directly if you're heavily dependant on them. WP-Cron does this sort of thing, I think. So does the Sidebar Widgets plugin.

    And that's basically how you do it.

  3. mavico
    Member
    Posted 7 years ago #

    thx alot .. i`ll test that!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.