Skip to content

Instantly share code, notes, and snippets.

@norcross
Last active July 11, 2017 19:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/b15c78fef164e851519084917a61b31a to your computer and use it in GitHub Desktop.
Save norcross/b15c78fef164e851519084917a61b31a to your computer and use it in GitHub Desktop.
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