WordPress plugin executing code twice
I am building a WordPress plugin and am stuck on an 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 to 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 a difference of 7 sec. My code is running twice in the interval of 7 sec.
I do not understand why this is happening.