8

Does anyone know a non-hacky way of getting the plugin directory path from within a theme's functions.php?

I've used plugin_dir_path(FILE), but because its called in the theme's functions.php it returns the path to that file. Not the plugins directory. I could string manipulate it and add the plugins path, but that feels nearly as bad as hardcoding the whole path.

I have got it working using:

require_once( ABSPATH .'/wp-content/plugins/ehu-events/event-widget.php' );

But I know that's so oldschool and wrong and I'd probably be shunned from the wordpress community for using it!

Is there a standard wordpress function I can use for this?

17

Maybe what you're looking for is :

WP_PLUGIN_DIR  // full path, no trailing slash
WP_PLUGIN_URL  // full url, no trailing slash

See documentation

  • 1
    Keep in mind that there're filters that might move the location somewhere else. This might be best bet, but it's surely not a save assumption. – kaiser Oct 9 '13 at 11:15
  • 2
    The linked documentation says the following about the constants: «These should not be used directly by plugins or themes, but are listed here for completeness.» So it probably shouldn’t be suggested as the solution, when the advice is to not use them in your theme. – Gchtr Mar 30 '18 at 9:59
7

I believe this may be more reliable than depending upon the constants.

$url = plugins_url();
$path = parse_url($url);
var_dump($path['path']);

The reason I suspect it may be more reliable is that the plugins_url function considers both the WPMU_PLUGIN_URL and the WP_PLUGIN_URL constants and will also respect the plugins_url filter.

  • 1
    This is the most non-hacky method and it should be the best answer. – CGeorges Mar 15 '16 at 23:06
  • what about plugins contained within themes? – brett Jan 3 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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