$wpdb->insert is running multiple times on page load, but only called once

It’s hard to say why without seeing more of your code. So if you’re interested in knowing why, then I’d encourage you to share more of your code.

If you’re only looking for a solution, then something like the following will probably work. (I say probably because I don’t know because I don’t have your code.) What we want to do is make sure that the code to insert the table is executed once and only once. Because we’re inserting the table on the init hook, the register() method will only fire once because init only fires once. So the conditional check is really unnecessary, but now we know for extra sure that the table will only be added once.


functions.php

require_once( PATH_TO . '/class-wpse106269.php' );
add_action( 'init', [ wpse106269::getInstance(), 'register' ] );

class-wpse106269.php

<?php
class wpse106269 {
  private static $instance;
  protected $table_already_inserted;
  protected function __construct() {}
  protected function __clone() {}
  protected function __wakeup() {}

  public static function getInstance() {
    if( ! isset( self::$instance ) ) {
      self::$instance = new self;
    }
    return self::$instance;
  }

  public function register() {
    if( ! $table_already_inserted ) {
      $this->insert_table();
      $this->table_already_inserted = true;
    }
  }

  protected function insert_table() {
    //* Insert table here
  }

Add:

I placed this in my functions.php with only ACF (free) active. The init hook only runs once. So this is either a problem with ACF (pro) or there’s another plugin that’s still active that doesn’t play nicely with ACF (Pro?).

//* Don't access this file directly
defined( 'ABSPATH' ) or die();

add_action( 'init', [ new wpse106269(), 'init' ] );

class wpse106269{
  protected $n = 1;
  public function init() {
    echo $this->n;
    $this->n = 1 + $this->n;
  }
}