Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current plugin directory in WordPress?

I need to get the current plugin directory like:

[wordpress_install_dir]/wp-content/plugins/plugin_name 

(If getcwd() is called from the plugin, it returns [wordpress_install_dir], the root of installation.)

like image 367
bog Avatar asked Jun 27 '10 20:06

bog


People also ask

How do I find my current WordPress plugin path?

I would suggest to use a WordPress internal constant to solve this case: $my_plugin = WP_PLUGIN_DIR . '/my-plugin'; if ( is_dir( $my_plugin ) ) { // plugin directory found! }

How do I find the location of a plugin?

Your browser's C: drive plugins directory folder should be under your username and associated with the browser. For example, the Chrome plugins directory folder could be located at “C:\Users\UserName\AppData\Local\Google\Chrome\Application\Plugins” (without quotation marks).


1 Answers

Use the WordPress core function that's designed specifically for that purpose:

<?php plugin_dir_path( __FILE__ ); ?> 

See the Codex documentation here.

You also have

<?php plugin_dir_url( __FILE__ ); ?> 

if what you're looking for is a URI as opposed to a server path.

See the Codex documentation here.

IMO, it's always best to use the highest-level method that's available in core, and this is it. It makes your code more future-proof.

like image 178
Tom Auger Avatar answered Oct 02 '22 12:10

Tom Auger