Skip to content

Instantly share code, notes, and snippets.

@danixland
Last active December 18, 2015 07:39
Show Gist options
  • Save danixland/5748568 to your computer and use it in GitHub Desktop.
Save danixland/5748568 to your computer and use it in GitHub Desktop.
A small script to batch add users to a WordPress website DO NOT USE IN PRODUCTION ENVIRONMENT!!!

usersbatch.php

script for batch adding users to a WordPress website.

DO NOT use in production environments.
This is just for testing purposes.

USE AT YOUR OWN RISK!
DO NOT BLAME ME IN CASE OF DAMAGES!!

original script seen on [worpress.stackexchange.com] 1

Modified but still not Idiot proof!! :-)

USAGE:

http://localhost/usersbatch.php?lock=0&site=wordpress&limit=N

In this example you'll create N users on the site http://localhost/wordpress where N is the number you set, minus the id of the last user that you created on that site.

If you have just one user with id 1 the script will create N-1 users, while if you have one user with id 5 it will create N-5 users.

NOTICE:
If $limit is smaller than the id of the latest user you created the script won't run.

<?php
/**
*
* usersbatch.php - script for batch adding users to a WordPress website.
*
*/
//true = script disabled (locked)
$lock = ( isset($_GET['lock']) ) ? $_GET['lock'] : true;
// which site you want to add the users to?
$site = ( isset($_GET['site']) ) ? $_GET['site'] : '' ;
// extra lock - if $limit is false the script won't run
$limit = ( isset($_GET['limit']) ) ? $_GET['limit'] : false;
// if the script is not locked
if ( ! $lock ) {
if (false !== $site ) { // if we have many sites to work with
require_once( $_SERVER['DOCUMENT_ROOT'] . '/' . $site . '/wp-load.php' );
} else { // we assume that wp-load.php is in the same directory as usersbatch.php (aka this is a single WP install)
require_once( 'wp-load.php' );
}
$last_registered_user = $wpdb->get_row("SELECT ID FROM $wpdb->users ORDER BY ID DESC LIMIT 1;");
$new_id = $last_registered_user->ID + 1;
if ( $last_registered_user->ID >= $limit ) {
echo '<h1 style="color: red;">limit is smaller than the id of the last user you created, exiting.</h1>';
exit;
}
echo "<h1>Creating users for site " . $site . "!</h1>";
// if we have a limited number of users to create
while($new_id <= $limit) {
// wp_create_user( 'username', 'password', 'email address' );
wp_create_user( 'user_'.$new_id, 'changeme', 'user'.$new_id.'@example.com' );
$new_id++;
echo "<h3>User 'user_" . $new_id . "' created!</h3>";
}
echo '<h1 style="color: green;">Success!! you have now ' . $limit . ' users.</h1>';
// the script is locked or we didn't set a site to use
} else {
echo '<h1 style="color: red;">This script is locked to prevent accidental use.</h1>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment