wpdb->insert creates duplicate rows

We had a similar problem here. The reason was that when loading a page in the frontend, WordPress actually was doing two requests:

  • One to load the page
  • A second one in the background after the page was loaded using admin-ajax.php

The solution was to make sure, out function was only executed during the “normal” page-load and not the AJAX request:

function my_callback() {
   if (defined('DOING_AJAX') && DOING_AJAX) return;
   // do normal stuff here
}