Here is another approach in which we can answer the question, here is a small Plugin that adds a Submenupage for “Users”, containing a small form where you can enter a username to create the new user with no emailadress.
Additional Error handling allows you to see if the current user already exists, as usernames have to be unique.
Create a add-anonymous-user.php
file in your plugins directory, and place this code inside – activate the plugin and voila.
Have fun!
<?php
/*
Plugin Name: Add anonymous user
Description: Add a user with no email adress
Version: 0.0.1
Plugin URI: http://fischi.cc
Author: Thomas fischi! Fischer
Author URI: https://fischi.cc
Domain Path: /languages/
License: GPL v2 or later
*/
/* Add a Menu Page */
add_action( 'admin_menu', 'f711_register_menu_page' );
function f711_register_menu_page() {
add_submenu_page( 'users.php', 'Add anonymous User', 'Anon User', 'manage_options', 'anon_user', 'f711_menu_page' );
}
// Function to display the input
function f711_menu_page() {
echo '<div class="wrapper">';
//check if form was sent
f711_check_for_input();
echo '<h1>' . __( 'Add Anon User', 'aau' ) . '</h1>';
echo '<h3>' . __( 'Username', 'aau' ) . '</h3>';
// insert a small form that targets this page
echo '<form method="POST" action="">';
echo '<input type="text" placeholder="' . __( 'Insert User Name', 'aau' ) . '" name="anonname" />';
echo '<input type="submit" value="' . __( 'Create', 'aau' ) . '" />';
echo '</form>';
echo '</div>';
}
// Function to process the input
function f711_check_for_input() {
// do nothing if Form was not sent
if ( !isset( $_POST['anonname'] ) || $_POST['anonname'] == "" ) return;
// sanitize the input to avoid security breaches
$username = sanitize_text_field( $_POST['anonname'] );
// define the new user
$args = array(
'user_login' => $username,
'user_pass' => md5( rand( 1000000, 9999999 ) ), //create hash of randomized number as password
'role' => 'subscriber'
);
// try to insert the user
$user_id = wp_insert_user( $args );
// check if everything went coorectly
if ( !is_wp_error( $user_id ) ) {
//add a small notice
echo '<div class="updated"><p>' . __( 'User created', 'aau' ) . ': ' . $username . '</p></div>';
} else {
//show error message
$errors = implode( ', ', $user_id->get_error_messages() );
echo '<div class="error"><p>' . __( 'Error', 'aau' ) . ': ' . $errors . '</p></div>';
}
}