wordpress get plugins directory code example

Example 1: how to get plugin directory path in wordpress

//current path: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
$dir = plugin_dir_path( __DIR__ );
//$dir is set to /home/user/var/www/wordpress/wp-content/plugins/

Example 2: how to get plugin directory path in wordpress

if ( is_admin() ) {
    include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' );
} else {
    include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' );
}

Example 3: how to get plugin directory path in wordpress

foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) {
    include_once $file;
}

Tags:

Php Example