WordPress plugin executing code twice

I am building a WordPress plugin and stuck on a unexpected problem where my plugin code is running twice.

Please see my plugin code:

/* Plugin Name: plugin name
Plugin URI: 
Description: Descript
Version: 1.0
Author: Author
Author URI: 
License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

require_once(dirname(__FILE__) . '/lib/visit_stats.php');

Here is my visit_stats.php code:

// Current wordpress user id
  $userid = 3;

// Getting visit value from user meta
$oldvisitdata = get_user_meta($userid, "visits", true);

// For example if it's returning 3

// Now increasing the old visits value by 1
$newvisit = $oldvisitdata + 1;

// Now update the meta
update_user_meta($userid, 'visits', $newvisit);

When we retrieve the value it should be 4, but instead it’s 5, which means when I +1, it’s adding 2 on the old value. I think it’s only because the script is running twice.

I did a test to confirm it, so here is the output of the time: <date=12:36:34 am> <date=12:36:41am>. When I open my website, my plugin should execute the code only once, but it’s doing it twice, as there is difference of 7 sec. My code is running twice in the interval of 7 sec.

I do not understand why this is happening.

2 s
2

Quite simply, it’s loading more than once because you’re probably actually making more than one HTTP request to “WordPress”.

You should not do anything like counting on a plugin load like this, because WordPress can load in many different situations, sometimes more than once for even a single page view.

The wp-cron process, for example, can run in the background on another request in order to handle things like scheduled posts, update checks, trash cleanup, and other such mundane tasks. Those HTTP requests do indeed load plugins, so that plugins can do things when the wp-cron process is called.

Elements of the main Dashboard, such as the “WordPress News”, are shown using Javascript. These are separate requests in the backend to the admin-ajax routines, which load WordPress in order to access the HTTP functions, and then to retrieve data from the WordPress.org RSS feeds. These load most of WordPress, plugins included.

So what you’re counting is not so much a “visit” counter as it is a “hit” counter, and one visit can be more than one hit. If you actually want to count visits, you should count much more selectively, ignoring things like background requests.

  • When a cron process is running, DOING_CRON will be defined.
  • When an AJAX request is running, DOING_AJAX will be defined if it’s going through the admin-ajax system. However, note that many plugins and themes use their own system of callbacks to WordPress, and may not be easily recognizable like this.

So, you can ignore those hits in your counter. But realistically, you’re still going to get “false” count increases from other plugins and themes. So you should instead change your counting method to not count on plugin load, but instead to count on specific WordPress action hooks that you’re wanting to monitor.

Leave a Comment