Wordpress get plugin directory

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


Code For Plugin Root Path

$dir = plugin_dir_path( __FILE__ );
// Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/

Code for plugin path

echo  WP_PLUGIN_DIR.'/plugin-name';

Yeah as per description of plugin_dir_path it will give you current plugin file path. But as per what you asking here you can do something like below unfortunately no direct way,

$plugin_dir = ABSPATH . 'wp-content/plugins/plugin-folder/';

Edit: 18-09-2021

The best practice is to use with latest version is WP_PLUGIN_DIR as follow:

$plugin_dir = WP_PLUGIN_DIR . '/plugin-folder';

Tags:

Wordpress