How can I un-reserve a pending username registration?

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', )
);

Leave a Comment