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 includes 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 i'm doing wrong ?
thanks
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 files of your plugin. Use code reference below: