How to get path or root of plugin folder, not file or dir?

I want to include one file from the plugin root to somewhere in the plugin folder. Folder structure:

/plugin
  /folder - Can't 'esacpe' from this folder to root
    /otherfolder
      /req-file-to-here.php // fetch here
  /req-file-from-here.php // send from here

What to exactly type in file req-file-to-here.php ? I have tryed something like this:

require plugin_dir_path( __FILE__ ) . '..path to file';

or

require plugin_dir_path( __DIR__ ) . '..path to file';

Not working. Help :)

Topic paths directory plugin-development plugins Wordpress

Category Web


How about just define a constant that stores the plugin's root path?

Define path constant

For calling numerous files, it is sometimes convenient to define a constant:

define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'includes/admin-page.php');
include( MY_PLUGIN_PATH . 'includes/classes.php');
// etc.

— See https://developer.wordpress.org/reference/functions/plugin_dir_path/#comment-491

So in your main plugin file:

define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

And then in folder/otherfolder/req-file-to-here.php, do:

require MY_PLUGIN_PATH . 'req-file-from-here.php';

Alternatively, you could define just the path to the main plugin file:

define( 'MY_PLUGIN_FILE', __FILE__ );

And then in folder/otherfolder/req-file-to-here.php, do:

require plugin_dir_path( MY_PLUGIN_FILE ) . 'req-file-from-here.php';

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.