I’m trying to write a plugin with multi files. I’m sure I did it before without a problem, but now I have the problem in the subject.
In the main plugin file I included a file name – ydp-includes.php. Inside of ydp-includes.php I included all the files I wanted like this:
<?php
include(dirname(__FILE__) . '/1.php');
include(dirname(__FILE__) . '/2.php');
include(dirname(__FILE__) . '/3.php');
include(dirname(__FILE__) . '/4.php');
?>
But I’m getting:
Fatal error: Call to undefined function add_action()
The files are included, but for a reason I can’t see at the moment WordPress doesn’t see them as one plugin package and each WordPress function inside ignored.
Is there another best practice way to develop multiple files WordPress plugin? What am I doing wrong?
In PHP
include
is a statement, not a function.So it should be:
Or to be perfect:
Based on the error message, it sounds like you’re trying to access the plugin file directly, which is incorrect. WordPress uses a front-controller design pattern, which means that you’re going to want to have your files like this:
Inside of the my-plugin-name.php:
That will add a WordPress admin menu item, and load the required files. You’ll also be able to require more files inside of the included files now, using the constant
MY_PLUGIN_PATH
See also:
add_menu_page
plugin_dir_path()
Use
plugin_dir_path( __FILE__ );
to get the files of your plugin. Use the code reference below: