Plugin main file is constantly been fired on every website visit

Question

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.

0
minasg 3 months 2022-04-29T15:02:36-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse