'My wordpress plugin is beeing called twice
I'm creating a WordPress plugin, and by debugging, I noticed its being called twice on each request. My plugin code is like this:
class Br1E_EngenhariaPlugin{
(...)
}
new Br1E_EngenhariaPlugin(); // A breakpoint here is called twice on each request
Debugging the code I see that the problem is that the wp-load.php file is being called twice. The first time the callstack started at index.php:
If I hit continue, it will stop again at the same breakpoint, this time the callstack started at wp-cron.php:
I tried to make my class a singleton, by using a static variable to ensure the class is loaded only once:
class Br1E_EngenhariaPlugin{
public static function LoadOnce() : Br1E_EngenhariaPlugin
{
if (self::$pluginInstance == null)
self::$pluginInstance = new Br1E_EngenhariaPlugin();
//register_activation_hook( __FILE__, array($br1Engenharia, 'install') );
return self::$pluginInstance;
}
(...)
}
Br1E_EngenhariaPlugin::LoadOnce();
But it didn't work. The $pluginInstance static variable is null on the second time it's called, it's like it's a different request.
Solution 1:[1]
And yes, it is a different HTML request called from spawn_cron() in the file wp-includes/cron.php:
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
Here $cron_request['uri'] contains http://your-site.com/wp-cron.php. And you can see it in the second call stack.
wp_cron in the file wp-includes/cron.php is hooked on every' init' event. Then, if any cron jobs are to be executed, spawn_cron() is called, producing an independent HTML request to the site.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karl Hill |


