Get a path to a different plugin

To get a the path to a plugin you can use plugin_dir_path(__FILE__) – but this obviously has to be called from within the plug-in.

How can you reliably get the path to plugin B (pluginb/pluginb.php) from within plug-in A?

Read More

Edit: Its assumed you know the slug of the plug-in you’re after (actually you can get them from get_plugins()). But I would like it to work generally.

Related posts

Leave a Reply

3 comments

  1. My best guess would be:

    if ( ! is_file( $dir = WPMU_PLUGIN_DIR . '/pluginb/pluginb.php' ) ) {
        if ( ! is_file( $dir = WP_PLUGIN_DIR . '/pluginb/pluginb.php' ) )
            $dir = null;
    }
    
    return $dir;
    

    However, the danger here is still the assumption of the plugin’s “basename” – a well written plugin will still function even when its directory and/or main file has been renamed (for whatever reason).

    Which goes back to my original comment – depending on which third-party plugin this is referring to, many authors define their own methods/constants to hold the plugin path – it would make sense to check for their existence & use these instead (if available).

  2. So one method is to call plugin_dir_path() within the current plug-in and replace your own plugin directory name with that of the slug of the plug-in you’re after (pluginb/pluginb.php):

    So within our plug-in plugina/plugina.php,

    $plugin_b = str_replace('plugina/','pluginb/pluginb.php',plugin_dir_path(__FILE__));
    echo $plugin_b; //Prints path/to/pluginb/pluginb.php';