Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quangmai911/9a3844c67c8f3f76a56d1b7debbe7b2c to your computer and use it in GitHub Desktop.
Save quangmai911/9a3844c67c8f3f76a56d1b7debbe7b2c to your computer and use it in GitHub Desktop.
Code snippet to reject login for admin/authors from the regular login url
<?php
/*
* Code snippet to reject login for admin/authors from the regular login url
* In this example, people who have edit_posts permission is require special url string to login
* The URL need to be: https://yourdomain.com/wp-login.php?salt=your_random_url_string
* For normal users they can login without the special salt
* But If author or admin try to login it will reject the authentication
*
*/
add_action('wp_login', function ($username, $user) {
// your target salt. Feel free to customize this
$urlParamString = 'your_random_url_string';
if (user_can($user, 'edit_posts')) {
$refer = wp_get_referer();
if (!strpos($refer, $urlParamString)) {
wp_logout();
wp_redirect(wp_login_url());
exit();
}
}
}, 1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment