Redirect wp-admin to the Homepage for Logged-Out Visitors
Sends logged-out visitors who open /wp-admin/ to the homepage instead of the login page, so bots probing wp-admin do not learn where to log in.
Opens WPIDE on
Code
add_action( 'init', function () {
global $pagenow;
if ( ! is_admin() || is_user_logged_in() || wp_doing_ajax() || wp_doing_cron() ) {
return;
}
// Entry points that must stay reachable for visitors.
if ( in_array( $pagenow, array( 'admin-ajax.php', 'admin-post.php', 'async-upload.php' ), true ) ) {
return;
}
wp_safe_redirect( home_url( '/' ) );
exit;
}, 1 );
Notes
You then need to open wp-login.php directly to log in.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.