1
  1. I created a php class on a separate file
  2. I included the file in functions.php
  3. I initialized the class

The class among many other things has one functions that does this:

$data = array(
    'time' => date('Y-m-d H:i:s'),
    'offset' => 0,
);

$format = array(
    '%s',
    '%d'
);

$wpdb->insert('my_custom_table',$data,$format);

One page load the class runs, and for some reason I get 2 entries in the database instead of one. The data is correct, but I don't know why I have 2 entries if I only inserted once.

In my class $wpdb->insert only runs once. I can test that buy echoing anything right next to it.

troubleshooting:

So I removed $wpdb->insert outside the class and added directly to functions.php. I noticed that the $wpdb->insert was running 2 or more times.

What's causing it to run multiple times?

No, I don't have a loop. Yes, I know I should not run it in functions.php, but it' for testing only.

I have found similar questions, but no good answer.

0

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;
  }
}
  • I didn't think about using it on a hook. I will give that a try. – gdaniel Feb 17 '17 at 21:15
  • this is just bizarre. I added it to the init hook, and I still get multiple entries. The amount of entries seem to vary depending on how long the page loads. Going to start disabling plugins. – gdaniel Feb 17 '17 at 21:22
  • The problem is actually ACF Pro. For some reason when ACF Pro is on, wpdb->insert runs two or more times. – gdaniel Feb 17 '17 at 21:26
  • That's odd. I'm looking at ACF (non-pro) to see if I can see what might be causing the issue. – Nathan Johnson Feb 17 '17 at 21:33
  • Thanks for taking a look. I am going to try the same script later on a fresh site, without any additions and pin point the issue. – gdaniel Feb 18 '17 at 0:18
0

You most likely have a plugin or a theme that is making a separate call to WordPress in the page content, to do something like retrieve CSS or Javascript code. The page loads, and then the stylesheet or JS loads from inside the page in a way that causes WordPress to load a second time.

Alternatively, you have something using the wp-cron system which is causing wp-cron to spawn and thus load WordPress up again in a secondary process (to run whatever the wp-cron action is). This can cause the same effect, especially if you have something using wp-cron on a short timer.

The upshot of it is that WordPress can get loaded for many reasons, and not all of them are to render the page as HTML. So, any time you're "doing something" from WordPress loading, then you probably don't want to put it on the init hook, or you want to have a more specific check so that you're not just arbitrarily doing that thing every single time.

  • True, I assumed that he wanted it run every time WordPress was loaded. Even so, why would it only run once when placed directly in functions.php then? – Nathan Johnson Feb 17 '17 at 22:37
  • For testing. So that I don't have to setup something else just to run some tests first. Anyway, If the code runs twice, I would expect other parts of the code to also run twice, but that doesn't happen. It's only the $wpdb->insert that's running twice. – gdaniel Feb 17 '17 at 22:43

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.