0

I am creating my first wordpress plug, and am using the WPBP template which is a great timesaver.

In the main of the plugin (called myplugin.php), I added a simple command to write a line of text to a file. That works, however, when I refresh the page (one page of my website), I see approx 4 lines written to my file.

Does the myplugin.php get included / execute many times for each page? If so, why?

Next, what is the best way to enforce only running once per page. I plan to create a PHP object and don't want to create it 4 time. Obviously I could build my own detection if the class exists, but that seems wrong. Surely WP has devised some mechanism to ensure plugin global/main code only runs once.

0

Plugins are only loaded once per request. Plugins are loaded with this code:


// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
    wp_register_plugin_realpath( $plugin );
    include_once( $plugin );
    /**
     * Fires once a single activated plugin has loaded.
     *
     * @since 5.1.0
     *
     * @param string $plugin Full path to the plugin's main file.
     */
    do_action( 'plugin_loaded', $plugin );
}

This is inside wp-settings.php, which only loads once, but also note that include_once is used, meaning that the plugin couldn't be loaded twice even if that code ran twice for some reason.

However, not that I said per request. This means that any AJAX requests that are sent from the page will cause the plugin to be loaded for each of those AJAX requests, in the background. This could explain why the function is running multiple times

3
  • I suspect that's it - AJAX requests. So what is the proper way to handle this? My plugin's global code (including hook & unhook code in the boilerplate) is running 4-7 times per website page. What do plugin developers normally do to handle this situation? – TSG Sep 30 '19 at 0:58
  • Why do you need to handle it? What does your code do? You mention creating an object, but I don't see why this is a problem, since PHP objects aren't persistent across requests anyway. – Jacob Peattie Sep 30 '19 at 1:54
  • GOod point - perhaps I will ignore for now. – TSG Sep 30 '19 at 3:11
0

In this case you can attach your code functionality to a hook, one of the most common for this case is plugins_loaded. You can see more details about this hook on the codex.

Like:

<?php 

add_action( 'plugins_loaded', function() {
  $my_plugin = new Plugin();
  $my_plugin()->init();
});

Another approach is use the Singleton pattern for your object class so you can update to an approach like:

<?php 
$my_plugin = Plugin::getInstance();
add_action( 'plugins_loaded', [ $my_plugin, 'init' ]);
4
  • But won't the add_action run 4 times? (Adding my function to the same call-back 4 times)? So same problem. Do you know WHY this is happening? – TSG Sep 30 '19 at 0:43
  • No it should run only once, The simplest way to replicate the problem is to create a new directory on the plugins directory and a single file with the name of the file and try to paste the code above it might be an issue with the way your files are being loaded or maybe your objects are created. – Crisoforo Gaspar Sep 30 '19 at 0:56
  • I created a simple plugin file, and append to log when global code runs. Same result, multiple lines written per page view. – TSG Sep 30 '19 at 1:20
  • I also tried to plugins_loaded approach, and that code is running 4 times as well. Still something fundamental I'm missing – TSG Sep 30 '19 at 1:30

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.