Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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.

share|improve this question
    
Would this apply to any plugin? or one/several in particular? If the latter, are they your own, or public releases? –  TheDeadMedic Nov 10 '12 at 13:11
    
So I'm assuming you know the slug of the plug-in you're after: e.g pluginb/pluginb.php, but its third-party. –  Stephen Harris Nov 10 '12 at 13:20
add comment

2 Answers

up vote 3 down vote accepted

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).

share|improve this answer
    
Ah ok, I've updated the question :). You can get the plug-ins slug from get_plugins() - in my use case there is enough. If you are after some specific plug-in, and nothing better is available, I guess you can search for the plug-in via name in get_plugins(). –  Stephen Harris Nov 10 '12 at 13:56
    
+1 Would ideally like to avoid using constants - but this solution at least checks when using them :) - and is how core seems to do things. –  Stephen Harris Nov 10 '12 at 13:59
add comment

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';
share|improve this answer
    
I would advise against this, since there is the assumption that plugin B is in the same directory as A (which won't neccessarily be the case, ala MU plugins). –  TheDeadMedic Nov 10 '12 at 14:04
    
Thanks, thought there would be something that I'd miss :) –  Stephen Harris Nov 10 '12 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.