# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: security, admin, login
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-wp-admin-for-guests/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect wp-admin to the Homepage for Logged-Out Visitors" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
