0

Here's a very simple plugin, which I drop into mu_plugins in my multisite installation:

<?php
/*
  Plugin Name: Test
 */

class myclass {
    function __construct() {
        error_log("I just punched myself in the head.");
    }
}  // end of class definition

/* start of plugin functions */
function run_plugin() {
    static $counter = 0;
    if (0 === $counter++) 
        $i = new myclass();
}

/* start of execution code */
add_action('init','run_plugin');

OK - I log into WP admin, and I switch pages twice -- go to media library then themes. Counting the first landing on index.php there are three page renderings total.

Output of this plugin:

I just punched myself in the head.
I just punched myself in the head.
I just punched myself in the head.

So, given the static counter in the calling function - why is the constructor getting called three times? Or, is WP destroying my plugin and reloading it with every page load?

I'm very confused.

2 Answers 2

1

The init action will fire once on every page load. PHP doesn't maintain state between requests. If you want to run something once and never again forever unto eternity, you need to store something that persists between requests, like an option in the database, and check that before doing that one-time thing.

1

Unfortunately, this is not how PHP works. The PHP program is entirely executed (from start to finish) during a single request, i.e. when someone loads a page. Once you click any link (go to another page in your website) - everything is destroyed (every variable, class instance etc.) and the entire script starts from scratch.

In case of WordPress - the entire WordPress environment is loaded and executed every time someone requests a page. Every WordPress action (like the one you hooked into - 'init') is executed, all plugins and classes instantiated.

There is, of course, caching happening on the server side, but this is an advanced topic and probably not something you are interested in as a beginner.

As Milo said, if you want something to persist from page load to page load you have to store it in the database, most probably using transient API (if this is some kind of data other than, for example, a setting or post). If what you are doing is something that you would like to run once per user - you can store it in your user's browser as a cookie.

2
  • Thank you Mateusz - but I think you meant to say "this is not how WP works" -- since PHP actually does work that way (if the current instance is not destroyed). Anyway, I understand now thanks.
    – C C
    Nov 25, 2015 at 3:55
  • @CC no, he is correct in saying that is not how PHP works. You have separate requests, PHP can't retain data across requests.
    – Milo
    Nov 25, 2015 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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