Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Force a redirect if a certain username is attempted.
<?php
add_filter( 'authenticate', 'rkv_disable_username_logins', 20, 3 );
/**
* Disable admin username and redirect to Google (or another site).
*
* @param object $user The WP_User object.
* @param string $username The entered username.
* @param string $password The entered password.
*
* @return object $user The WP_User object, or redirected.
*/
function rkv_disable_username_logins( $user, $username, $password ) {
// If we have no username to check, just bail right away.
if ( empty( $username ) ) {
return $user;
}
// Set our array of usernames to check for.
$names = apply_filters( 'rkv_disabled_username_list', array( 'admin' ) );
// Bail if the name is empty.
if ( empty( $names ) ) {
return $user;
}
// Cast our names list to be an array (in case someone messed up the filter).
$names = (array) $names;
// Set our array of usernames to check for.
$site = apply_filters( 'rkv_disabled_username_site', 'https://www.google.com' );
// Only run this on the names set.
if ( in_array( $username, $names ) ) {
// Double check we have a site to use.
$site = ! empty( $site ) ? $site : 'https://www.google.com';
// Redirect to our chosen site.
wp_redirect( esc_url( $site ) );
exit;
}
// Return the user.
return $user;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment