Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress get plugin directory

Tags:

wordpress

Is there any function that would return the full path of my plugin in WordPress?

Example is

path/wp-contents/plugins/myplugin 

I have tried plugin_dir_path(__FILE__) but returns the current dir.

like image 317
Cindy93 Avatar asked Dec 26 '13 06:12

Cindy93


People also ask

How do I find the Plugins directory in WordPress?

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

What is plugin directory?

A plug-in subfolder can contain a single or a combination of several files (such as configuration files, JSP or ASP.NET files, etc.) that contain software code specific to the customization.


1 Answers

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! } 

Alternative

The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:

$plugins_url = plugins_url(); $base_url = get_option( 'siteurl' ); $plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url ); // Now $plugins_dir is same as the WP_PLUGIN_DIR constant.  $my_plugin = $plugins_dir . '/my-plugin'; 

My opinion in this case is: Use the constant WP_PLUGIN_DIR

like image 167
Philipp Avatar answered Sep 30 '22 19:09

Philipp