3

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

1
  • The content_url() will take you to your wp-content folder and then you can traverse to plugins folder . plugins_url() Retrieves a URL within the plugins or mu-plugins directory.
    – Anoop D
    Aug 24, 2021 at 11:15

1 Answer 1

4

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';
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.