My custom code in my custom (class-based) plugin for some reason, writes the same record twice, when as you can see it's called once. This double write also happens even when I do the same type of insert into the wp_posts
table, so it's not specific to the wp_users
table. I'm baffled. Any ideas appreciated.
<?php
class InsertUserWP {
function dbase_insert_wp_user() {
global $wpdb;
$wp_users_table = $wpdb->prefix . 'users';
$newUser = $wpdb->insert(
$wp_users_table,
array(
'user_login' => 'deleteme_login_' . rand(1, 10000),
'user_pass' => md5('deletemmepass'),
'user_nicename' => 'deleteme_nicename',
'user_email' => 'mario' . rand(1, 10000) . '@email.com',
'user_status' => 0
)
);
echo 'new user ID is: ' . $wpdb->insert_id . '<br>'; // (returns the first ID on screen)
}
// call the above insert function
function dbase_call_user_insert() {
$this->dbase_insert_wp_user();
}
}
// Run the insert
$insertCompanies = new InsertUserWP(); //instantiate the class
$insertCompanies->dbase_call_user_insert(); // run the insert function within
functions.php
then a user is going to be created on every request, not page load, request. That means every AJAX request, every page load, every admin request, REST API request, cron jobs, even assets that you're running through PHP plugins would insert a user. As an aside, this could be dramatically simplified to just thedbase_insert_wp_user
function with the removal of the class, there's no object oriented programming here and no local state to store on the object so there's no reason for a class