0

Greetings to everyone!

I'm working on a WordPress plugin, and while debugging something with the error_log function in the plugin main file, I noticed that the main plugin file is constantly fired...

The plugin is more complex but it's stracture can be considered as:

myplugin:
-- myplugin.php
-- fileA.php
-- fileB.php

In myplugin.php the code is like this:

if (!defined("ABSPATH")) {
    exit();
}
include_once plugin_dir_path(__FILE__) . "fileA.php";
include_once plugin_dir_path(__FILE__) . "fileB.php";

class main_class
{
    static function __construct()
    {
        error_log("in install main_class");
        new FileAclass();
        new FileBclass();
    }
}
$test = new main_class();
error_log("after test");

and the fileA.php, fileB.php like this respectively:

class FileAclass{
    error_log("in class FileAclass");
    function __construct(){
    add_action(...)
    }
}

class FileBclass{
    error_log("in class FileBclass");
    function __construct(){
    add_action(...)
    }
}

Please, note that the above is just a pseudocode. Let me know if the original code might be helpful.

When I zip the plugin and upload it the activation is done without errors but the debug file is like this:

in install main_class
in class FileAclass
in class FileBclass
after test
in install main_class
in class FileAclass
in class FileBclass
after test
in install main_class
in class FileAclass
in class FileBclass
after test
...

and the above text is repeated while navigating in the website (front and in wp-admin).

What I have tried and didn't work:

  • with and without the register_activation_hook and __construct in the classes
  • with register_activation_hook and :: notation without __construct just by calling a install() static function in the classes
  • deleted everything in the main page and left only the error_log("some text"). That made me crazy... the same thing happened...
  • wrapped the $test = main_class(); code with if (class_exists) logic
  • installed the plugin in other domain with shared hosting instead of a VPS

Really, I have tried everything I could think of and find through google searches.

Any ideas why this happens??? Is the above behavior normal?

Thank you in advance.

1 Answer 1

1

Ofcourse! It always loads and runs the code in the main plugin file, that's by design and is how plugins are intended to work. Why would it work any other way? How would it know without being told which code to run when?

There are no sandboxes that ensure your classes only work in certain scenarios, they're still written in the PHP programming language. Computers are very literal, it always runs because you never told it not to run in certain situations.

WordPress will load your activated plugins and your themes functions.php files, which are just PHP files that get executed. Anything you do in those files will run, wether it's a frontend request, admin area, REST API, or even an RSS feed. It will run and any that is not in an action will immediately run, and it isn't sandboxed or cordoned off from the rest of WordPress or the other activated plugins. It's all loaded into the same memory at the same time in a single shared space.

So What Should I do?

What you need to do, is use actions to schedule when things are meant to happen. If you want to specify that something should only happen in a certain situation, you have to implement that yourself via hooks and if statements. Doing things in the main plugin files outside of actions other than adding actions and filters or loading more PHP files is ill-advised.

For example, you should only register admin pages when in WP Admin, so you would hook into the admin_init event and do work there, ensuring the code does not run on the frontend and other locations. Likewise you would register REST API endpoints for AJAX on rest_init. This also improves performance.

The rare exception is uninstall.php. If no uninstallation hook is registered it will load uninstall.php when a plugin is uninstalled. This is because you can't register an uninstallation hook if the plugin is not activated.

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.