I am setting up a new WordPress Multisite instance, and am still working out some kinks in the stack. Most significantly at the moment, user registration emails are not being delivered for some reason. As a result, several usernames are stuck in limbo as the confirmation emails necessary to activate them are lost.

For now I would like to just manually register these usernames without confirmation emails, but when I try to do so I get an error saying the name is reserved and may be available in a couple of days. How can I un-reserve them?

up vote 11 down vote accepted

DB Access layer & deleting rows

WordPress uses the wpdb class to manage access to the database layer using the global $wpdb. The class provides a method named delete() to delete rows from tables:

$wpdb->delete( $table, $where, $where_format = null );

Multisite tables & activation keys

WordPress has some MU specific tables, where one is {$wpdb->prefix}signups (prefix set in your wp-config.php file). Tables scheme here. Responsible for the user account activation is the activation_key, which gets set after the user clicked the link in the mail. After that, the activated key will get set with a datetime value. Before the account is activated, the default value will be 0000-00-00 00:00:00 (in case you need to query for the default). Also in the process is the tinyint/1 column active, which is set to 1 if a user is active.

Core itself uses wpmu_activate_signup(). Look at it for some examples. One is the following that updates a user entry to activate it - reworked for readability.

$wpdb->update(
    $wpdb->signups,
    array(
        'active'    => 1,
        'activated' => current_time( 'mysql', true ),
    ),
    array( 'activation_key' => $key, )
);

Building a query against not-yet-activated accounts

Simply let WP do the hard work:

global $wpdb;
$wpdb->delete(
    $wpdb->signups,
    array( 'user_login' => 'some_login', )
);

You could use the 3rd argument (array) if you are using a(n admin) form to perform those requests (for e.g. extending a WP_List_Table) to indicate that you are using a string. Keep in mind that you should still sanitize $_POSTed values. Hint: You could use the user_email as well.

global $wpdb;
$wpdb->delete(
    $wpdb->signups,
    array( 'user_login' => 'some_login', ),
    array( '%s', )
);

If you want a quick solution for deleting a signup in the database for a specific user, this should do what you want:

/**
 * Delete a row in the signups table for a given username.
 *
 * @param string $user_login Username.
 * @return bool Whether the signup row was successfully deleted.
 */
function delete_activation_key_by_user( $user_login ) {
    global $wpdb;

    $success = false;

    if ( false !== $wpdb->delete( $wpdb->signups, array( 'user_login' => sanitize_text_field( $user_login ) ) ) ) {
        $success = true;
    }
    return $success;
}

var_dump( delete_activation_key_by_user( 'the_username' ) );
// bool(true|false)

You can use also a plugin in the WordPress.org repo called 'User Activation Keys' to manage this problem while you're figuring out why emails aren't being sent. It lets you manually delete or approve activation keys for users in Multisite.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.