Cookie Settings

Choose which cookies to allow.

Full list in our Privacy Policy.

Cloudflare Firewall Rules for WordPress: 5 Custom Rules and What Each One Can Break

Requests for wp-login.php, xmlrpc.php, the REST API user list, PHP in uploads and wp-config stopped at Cloudflare, while admin-ajax.php and admin-post.php pass

Cloudflare firewall rules for WordPress are now WAF custom rules: Cloudflare upgraded Firewall Rules into custom rules (Firewall rules upgrade), and the Free plan allows five of them (Custom rules). Five WordPress addresses deserve one each: the login and dashboard, xmlrpc.php, the REST API user list, PHP files WordPress never runs, and copies of wp-config.php. Of the five guides that rank for this search, besides Cloudflare’s own docs, none covers the REST API user list or PHP in uploads. Lelops Advise creates these rules from a question in plain language, shows each call to Cloudflare before it runs, and saves a version of the zone you can roll back.

Before You Write Cloudflare Firewall Rules for WordPress

Custom rules live on your zone’s Security rules page, under Create rule > Custom rules (Create a custom rule), and run in the order you arrange them. Below Business, regular expressions are not available, and only Enterprise has the Log action (availability). So the five rules use wildcard, which ignores case and lets * match any characters, and lower(), both available on every plan (Operators).

Rules 1 and 3 let your team through by IP address. On your account’s Settings page, go to Lists, select Create new list, name it wp_team, choose the IP type and add your addresses; the Free plan includes one list (Lists). Keep that exception inside the rules: an IP access rule with Allow skips your whole WAF for those addresses.

Without Log, Cloudflare’s advice is to deploy a new rule with Managed Challenge, review what it matched in Security Events, then switch to Block. Leave out any rule built on cf.threat_score: Cloudflare now always sets that field to 0, so cf.threat_score gt 10 never matches (cf.threat_score). These rules add to the managed rules, which carry the signatures of known WordPress exploits.

Rule 1: Lock wp-login.php and wp-admin, Not admin-ajax.php

Rule 1: requests for wp-login.php and /wp-admin/ stopped unless they come from wp_team, with admin-ajax.php and admin-post.php left open

Rule 1 blocks the login page and the dashboard for everyone outside wp_team, and leaves out the two dashboard files the public side of WordPress calls. WordPress sends every AJAX request, from the front end too, to /wp-admin/admin-ajax.php (AJAX), and forms of visitors who are not logged in can post to /wp-admin/admin-post.php (admin_post_nopriv). WordPress’s hardening guide warns that protecting wp-admin/ can break the AJAX handler (Hardening WordPress). Three of the five ranking guides lock /wp-admin/ without that exception, and none leaves out admin-post.php.

(lower(http.request.uri.path) eq "/wp-login.php"
  or (http.request.uri.path wildcard "/wp-admin/*"
    and not lower(http.request.uri.path) in
      {"/wp-admin/admin-ajax.php" "/wp-admin/admin-post.php"}))
and not ip.src in $wp_team

Action: Block. It stops every login from outside the list: members or customers who sign in, password resets, and visitors unlocking a password-protected post, whose form posts to wp-login.php?action=postpass (get_the_password_form). If people outside your team log in, delete the last line, use Managed Challenge, and limit login attempts with a rate limiting rule (Free has one, counted per IP over 10 seconds). Keep admin-ajax.php out of any challenge: a challenge page fails when the browser expects a non-HTML answer, such as an AJAX or fetch request (Challenge Pages).

Rules 2 and 3: Block xmlrpc.php and the REST API User List

Rule 2 blocks xmlrpc.php, the endpoint WordPress keeps for remote publishing clients (XML-RPC) and pingbacks. Despite its name, WordPress’s xmlrpc_enabled filter only disables the methods that need a login; its own comment in WordPress’s code says pingbacks keep running (class-wp-xmlrpc-server.php). Each pingback makes WordPress fetch the page the caller names, which attackers turn against that page, as Cloudflare described in 2014 (Cloudflare blog). Password guesses sent to xmlrpc.php also escape a rate limit scoped to /wp-login.php.

lower(http.request.uri.path) eq "/xmlrpc.php"

Action: Block. It stops publishing apps and services that connect over XML-RPC, and pingbacks from other sites.

Rule 3 hides the REST API’s user list from everyone outside wp_team. Without a login, /wp-json/wp/v2/users lists every user with published posts (users controller), and each entry’s slug is, by default, the account’s login name in URL form (wp_insert_user). WordPress answers the same route at ?rest_route=/wp/v2/users (Routes and endpoints) and matches routes in any case (class-wp-rest-server.php). So the rule uses wildcard and decodes the query string, where Cloudflare’s URL normalization leaves %2F encoded (How URL normalization works).

(http.request.uri.path wildcard "*/wp-json/wp/v2/users*"
  or url_decode(http.request.uri.query) wildcard "*rest_route=/wp/v2/users*")
and not ip.src in $wp_team

Action: Block. It breaks the block editor’s author list for anyone editing from outside wp_team, since the editor reads authors from this route (post-author).

Rules 4 and 5: Block PHP and Files WordPress Never Serves

Rules 4 and 5: PHP in uploads, wp-includes and wp-admin/includes, copies of wp-config.php and debug.log blocked

Rule 4 blocks PHP files where WordPress never runs them, such as /wp-content/uploads/*.php. The web server can write to wp-content (Hardening WordPress), and the media library refuses .php files, since PHP is not among its file types (wp_get_mime_types), so a PHP file in uploads arrived another way. The other lines copy the list the hardening guide blocks with .htaccess, rewritten with wildcard; since * also matches /, the /wp-includes/*.php condition needs its not to stay out of subfolders, as WordPress’s own rule does.

(http.request.uri.path wildcard "/wp-content/uploads/*"
  and http.request.uri.path.extension eq "php")
or http.request.uri.path wildcard "/wp-admin/includes/*"
or (http.request.uri.path wildcard "/wp-includes/*.php"
  and not http.request.uri.path wildcard "/wp-includes/*/*")
or http.request.uri.path wildcard "/wp-includes/js/tinymce/langs/*.php"
or http.request.uri.path wildcard "/wp-includes/theme-compat/*"

Action: Block. On Multisite, delete the /wp-includes/*.php condition: the hardening guide notes that it stops ms-files.php from serving images.

Rule 5 blocks copies of wp-config.php and the debug log. The file holds the database connection details (wp-config.php), and editor or manual backups such as wp-config.php~ or wp-config.php.bak tend to be served as plain text (OWASP WSTG-CONF-03). With WP_DEBUG_LOG on, WordPress writes errors to /wp-content/debug.log by default, and its docs advise against debug tools on live sites (Debugging in WordPress).

http.request.uri.path wildcard "*wp-config*"
or http.request.uri.path wildcard "/wp-content/debug.log"

Action: Block. It also blocks a post such as /how-to-edit-wp-config/; with WordPress at the site’s root, "/wp-config*" still catches the copies. On the Free plan, join rules 4 and 5 with or into one rule to keep a slot free.

How to Deploy Cloudflare Firewall Rules for WordPress with Lelops Advise

A rule asked in Ask Lelops, confirmed by you, created as a Cloudflare custom rule and saved in Versioning, with Rollback

Lelops Advise answers questions about your Cloudflare setup and changes it through a chat, Ask Lelops, so the five rules go in from requests in plain language, with a version of the zone saved after each change.

  1. Give Lelops a token that can edit WAF rules. When you connect Cloudflare, the Resources & permissions screen lists what your API token allows. Lelops reads your configuration with a read-only token at minimum; to create custom rules, the token must also allow editing them, which Cloudflare’s API calls Zone WAF Write (permissions). Then open Ask Lelops from the right edge of any page in the Lelops app.
  2. See which slots are taken. Ask “List the WAF custom rules on example.com.” Before it calls Cloudflare, Ask Lelops shows the call it will make, with the zone, and runs it once you confirm. The answer lists the zone’s custom rules, so you know how many slots your plan has left.
  3. Create rule 1. Write “Add a custom rule on example.com named WordPress login and dashboard, with the action Block and this expression:”, then paste rule 1. Ask Lelops shows the call with the name, the expression and the action, and creates the rule once you confirm. Lelops also checks that your role in the organization allows the change. The expression names wp_team, so create that list in Cloudflare first.
  4. Create rules 2 to 5 the same way. Paste each expression with its action, and on the Free plan send rules 4 and 5 as one. Each new rule saves a version of the zone in Guard > Versioning (Lelops Guard), listed with its date and the person who made it.
  5. Check what the rules stop. Ask “Show the last security events on example.com.” Ask Lelops reads the zone’s 50 most recent security events, for the last 24 hours or the range you give, each with its action, the rule’s ID, the IP address, the path, the user agent and the country. A blocked request for /wp-admin/admin-ajax.php, or one from an address in wp_team, is a false positive to fix.
  6. See which rule acts most. Ask “Which WAF rules were triggered most on example.com in the last 24 hours?” Ask Lelops reads the top 10 rules by number of events, with each rule’s ID and action. To put a name on each ID, ask for the custom rules on example.com with their IDs.
  7. Fix a rule, or roll the zone back. Ask “Change the action of the rule WordPress login and dashboard on example.com to Managed Challenge”, or “Delete the xmlrpc.php rule on example.com.” Ask Lelops finds the rule’s ID, shows the change and runs it once you confirm. To undo a change, an Administrator opens Guard > Versioning, expands the version before it, and selects Rollback, then Restore and deploy: Lelops deploys that whole version of the zone again, so changes made after it to other parts of the zone, such as DNS records, go back too.

Lelops Advise writes these WordPress rules into your zone from a question in plain language, after your OK, and keeps a version of every change: lelops.ai/product/advise.

FAQ

How do Cloudflare custom rules let a WordPress team through by IP address?

Teams put their addresses in an IP list, such as wp_team, and end each rule with not ip.src in $wp_team, so the rule leaves those addresses alone and still applies to everyone else. The Free plan includes one custom list. An IP access rule with Allow does the same job too broadly: it takes those addresses out of custom rules, rate limiting and managed rules at once.

How many custom rules does the Cloudflare Free plan allow?

The Free plan allows 5 custom rules per zone, Pro 20, Business 100 and Enterprise 1,000. Below Business, rules cannot use regular expressions, and only Enterprise has the Log action. The Free plan also includes one custom list, enough for an IP list of the people who manage a WordPress site.

Should WordPress sites block xmlrpc.php on Cloudflare?

Sites that publish only from the WordPress dashboard can block it. The file serves remote publishing clients and pingbacks, and the setting most sites use to turn XML-RPC off leaves pingbacks running. A site that publishes from an app or connects a service over XML-RPC needs to keep it, and should protect its logins with a rate limiting rule instead. On the Free plan that is one rule, matched on the path and counted per IP over 10 seconds, so it has to cover both /wp-login.php and /xmlrpc.php.

Does blocking wp-admin on Cloudflare break WordPress?

It does when the rule also catches /wp-admin/admin-ajax.php and /wp-admin/admin-post.php. WordPress sends every AJAX request to admin-ajax.php, front end included, and visitor forms can post to admin-post.php. A rule that blocks the dashboard has to exclude both files, and a challenge is no fix, because a challenge page fails on AJAX requests.

Do custom rules replace Cloudflare's managed rules for WordPress?

No. Managed rules carry Cloudflare's signatures for known exploits, and even the Free Managed Ruleset includes rules for common WordPress exploits. On Pro and above, Cloudflare also recommends turning on the Cloudflare Managed Ruleset rules tagged for the CMS a site runs. Custom rules add what signatures can't know: which WordPress addresses a given site exposes and who is allowed to reach them.

Get started

Connect Cloudflare. See where your edge stands today.

Your first configuration health report, with the fixes to make first, in minutes.

  • No credit card required