Wordfence blocked 18.2 billion brute-force attacks against WordPress sites in Q2 2026 alone, according to its own threat intelligence report. Every one of those attacks knocked on the same door: your login page. Now imagine that door did not exist for anyone except you. That is what happens when you learn how to restrict wp-admin access by IP address — in about ten minutes.
The key takeaway: an IP allowlist is the strongest single layer you can add to WordPress admin access control, because it removes the login page from the internet for everyone except you.
How to Restrict wp-admin Access by IP Address — What It Actually Means
A wp-admin IP allowlist — a WordPress IP whitelist admin rule — is a server-level rule: only IP addresses you name may reach /wp-admin/ and wp-login.php. Everyone else gets a 403 error before WordPress even loads.
Unlike changing your login URL or limiting login attempts, which only slow attackers down, an IP restriction removes the target entirely — even if a plugin has a vulnerability or your password leaks.
Two honest limits. First, it only works well if your IP is stable — most home connections change IPs every few days, and a change locks you out until you update the rule. Second, it does not stop an attacker already on an allowed network, so keep two-factor authentication on too.
Before You Start: 3 Checks That Prevent a Lockout
Find your current IP address. Search “what is my IP” on Google and write it down.
Back up your site. Download a full backup first.
Check whether your IP is static or dynamic. A static IP is ideal; a dynamic one means using the fallback plan below or Method 4.
Method 1: How to Restrict wp-admin Access by IP Address with .htaccess (Apache)
This .htaccess restrict wp-admin method is the way to restrict WordPress admin by IP for most sites, since Apache powers most shared hosting and the rule runs before WordPress loads.
Create .htaccess inside your /wp-admin/ folder (not the root one) with your real IP instead of 203.0.113.25:
Require ip 203.0.113.25
<Files admin-ajax.php>
Require all granted
</Files>
That first line is the whole trick — and the part most tutorials still get wrong. Apache 2.4 replaced the old access-control syntax with the Require directive. The old Apache 2.2 syntax — Order Deny,Allow with Deny from all and Allow from — is deprecated, yet many guides still print it, and on Apache 2.4 it can silently fail. Use Require ip, not Order Deny,Allow. Add one line per extra address, or a CIDR range (Require ip 192.0.2.0/24).
The admin-ajax.php exception is not optional
Here is the mistake that breaks real sites: /wp-admin/admin-ajax.php sits inside the folder you just locked down, but contact forms and cart updates call it from your visitors’ browsers. Lock it down and those features fail silently. That is why the <Files admin-ajax.php> block re-opens that one file to everyone. Never restrict wp-admin without this exception.
Should you also block wp-login.php by IP?
Yes — add this to your root .htaccess file:
<Files wp-login.php>
Require ip 203.0.113.25
</Files>
Method 2: Nginx Location Block — For Nginx Hosts
If your server runs Nginx, .htaccess files do nothing — add this block inside your server {} block instead:
location = /wp-admin/admin-ajax.php { include fastcgi_params; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; }
location /wp-admin/ {
allow 203.0.113.25;
deny all;
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; }
}
location = /wp-login.php { allow 203.0.113.25; deny all; include fastcgi_params; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; }
Order matters. Nginx picks the most specific matching location, so the exact-match admin-ajax.php block must come first — otherwise the /wp-admin/ block swallows it and front-end features break. After editing, test with nginx -t and reload with systemctl reload nginx. Never reload without testing.
Method 3: A Small mu-Plugin Guard — When You Cannot Edit Server Files
If your host gives you no server access, enforce the same rule in PHP with wp-content/mu-plugins/ip-guard.php:
<?php
add_action( 'init', function () {
$allowed = array( '203.0.113.25', '198.51.100.7' );
$uri = $_SERVER['REQUEST_URI'] ?? '';
$is_admin = strpos( $uri, '/wp-admin/' ) === 0 || strpos( $uri, 'wp-login.php' ) !== false;
if ( ! $is_admin || strpos( $uri, 'admin-ajax.php' ) !== false ) {
return;
}
if ( ! in_array( $_SERVER['REMOTE_ADDR'] ?? '', $allowed, true ) ) {
wp_die( 'Access denied.', 'Forbidden', array( 'response' => 403 ) );
}
} );
The same admin-ajax.php exception applies in code as in .htaccess.
Method 4: Cloudflare Zero Trust / IP Rules — Best for Dynamic IPs
If your IP changes regularly, server-level rules become a chore. Cloudflare checks at the edge, before traffic reaches your server. Two options:
- WAF custom rule (free plan): In Cloudflare, go to Security → WAF → Custom rules and block requests to
URI Path contains /wp-admin/orwp-login.phpfrom IPs not in your list. - Cloudflare Zero Trust (free for up to 50 users): Create an Access application for
yoursite.com/wp-admin/*that allows only your email address — sign in once with a one-time code, and Cloudflare lets you through from any IP.
The 4 Methods Compared
| Method | Where it runs | Dynamic-IP friendly? | Best for |
|---|---|---|---|
.htaccess (Require ip) | Apache | No | Most shared hosting |
| Nginx location block | Nginx | No | VPS / managed Nginx hosts |
| mu-plugin guard | WordPress (PHP) | No | Hosts with no server access |
| Cloudflare Zero Trust / WAF | Cloudflare edge | Yes | Dynamic IPs, travelers |
The Dynamic Home IP Problem (and Your Fallback Plan)
Most home internet plans use dynamic IPs that change every few days. Plan for it:
- Keep a VPN with a static IP. Many VPN providers offer a dedicated IP add-on for a few dollars a month — allowlist only that IP.
- Use Cloudflare Zero Trust instead. As in Method 4, an identity-based login does not care about your IP at all.
The Cloudflare / Proxy Gotcha: REMOTE_ADDR Lies Behind a CDN
This is the subtle bug that makes IP restrictions fail silently on Cloudflare sites. When Cloudflare proxies your traffic, the connection to your server comes from a Cloudflare edge server — so REMOTE_ADDR sees a Cloudflare IP, not your real one. Your allowlist then either blocks you or allows every Cloudflare IP.
The fix is to restore the real visitor IP at the server first. On Apache, enable mod_remoteip with RemoteIPHeader CF-Connecting-IP, trusting only Cloudflare’s published IP ranges via RemoteIPTrustedProxy. On Nginx, use ngx_http_realip_module with real_ip_header CF-Connecting-IP; and set_real_ip_from entries for Cloudflare’s ranges. The golden rule: only trust a forwarded-IP header from a proxy you have explicitly listed as trusted.
Locked Yourself Out? Here Is the Recovery Path
Recovery takes two minutes — the rule lives in a file, not in the database.
Open your host’s File Manager or connect over FTP.
For .htaccess: delete the
Require iplines in/wp-admin/.htaccess(or the whole file).For Nginx: remove the
allow/denylines, runnginx -t, then reload Nginx.For the mu-plugin: delete
wp-content/mu-plugins/ip-guard.php.For Cloudflare: disable the WAF rule or Access policy in your Cloudflare dashboard.
Frequently Asked Questions
How do I restrict wp-admin by IP address?
Add an IP allowlist at the server level. On Apache, create /wp-admin/.htaccess with Require ip YOUR_IP plus an admin-ajax.php exception. On Nginx, use an allow/deny location block; a mu-plugin or Cloudflare WAF rule also works.
What happens if my IP address changes?
You get a 403 Forbidden error when you try to open /wp-admin/ or log in. Your site stays live for visitors. Recover by editing or deleting the rule through FTP or your host’s File Manager, then update it with your new IP. A VPN with a static IP or Cloudflare Zero Trust prevents repeat lockouts.
Does IP restriction work with Cloudflare?
Yes, but only if your server restores the real visitor IP first. Behind Cloudflare, your server sees Cloudflare’s IPs, not yours — so a plain Require ip rule would block you. Set up mod_remoteip (Apache) or ngx_http_realip_module (Nginx) with Cloudflare’s IP ranges as trusted proxies, reading the real address from CF-Connecting-IP.
Conclusion
Learning how to restrict wp-admin access by IP address is the closest thing WordPress security has to a cheat code: ten minutes of setup, and the login page simply stops existing for attackers. Use the correct Apache 2.4 Require ip syntax, keep the admin-ajax.php exception, and have a fallback ready.
Lock the door to everyone except you — then keep the spare key somewhere you can reach it. Pair it with our WordPress security guide, or let our team handle it — see our services or get in touch.