How to include wordpress plugins files

Solution:

The function plugin_dir_path(), can not be use like this with your file and folder configuration.

It returns an error because the file is not found.

plugin_dir_path(__FILE__)
Will get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in (file2.php in your case).

In your case, in file2.php, it will return : /path/wp-content/plugins/your-plugin/folder2/folder1/file1.php

The workaround:

In the main plugin file, you can add a define constant

defined(‘MYPLUGIN_DIR’) or define(‘MYPLUGIN_DIR’, plugin_dir_path( __FILE__ ));
Now MYPLUGIN_DIR is available in any file. In file2.php:

include_once( MYPLUGIN_DIR . ‘folder1/file1.php’);
Will return : /path/wp-content/plugins/your-plugin/folder1/file1.php

Hope it helps !