2

I'm trying to write a plugin with multiple 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 have included a file name - ydp-includes.php. Inside of ydp-includes.php I have 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?

2
  • Check out wppb.me for a good setup
    – jgraup
    Jan 2, 2016 at 4:56
  • Was this ever resolved? If so please do accept an answer that helped you, or write up the solution as your own answer and accept that. Thanks. Aug 9, 2016 at 21:02

3 Answers 3

1

This happens only if you call a file per HTTP without loading WordPress. That is something you should never do, because the plugin URL might be another domains where cookies don’t work anymore.

2
  • hello toscho thanks your for your reply. can you explaion more about what you mean: "call a file per HTTP"
    – largo
    Nov 12, 2012 at 13:04
  • If you are using the URL for one plugin file WordPress will not be loaded, just that file.
    – fuxia
    Nov 12, 2012 at 16:15
-1

I generally have just one PHP file in the plugin folder, then a sub-folder within which all my required PHP files sit. I use the following code in the main file to automatically include all files found in the inc directory:

foreach ( glob( dirname( __FILE__ ) . '/inc/*.php' ) as $file ) {
    require $file;
}
-1

Try this

<?php
include( plugin_dir_path( __FILE__ ) . '/1.php');
include( plugin_dir_path( __FILE__ ) . '/2.php');
include( plugin_dir_path( __FILE__ ) . '/3.php');
include( plugin_dir_path( __FILE__ ) . '/4.php');
?>
1
  • the core problem the OP have is what toscho says. with this kind of setup no wordpress API, even not plugin_dir_path is going to be available Oct 12, 2016 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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