{"id":52,"date":"2026-11-17T21:00:00","date_gmt":"2026-11-17T15:00:00","guid":{"rendered":"https:\/\/nazmulrobin.com\/blog\/?p=52"},"modified":"2026-11-13T23:57:53","modified_gmt":"2026-11-13T17:57:53","slug":"refactoring-bloated-plugins-with-ai","status":"publish","type":"post","link":"https:\/\/nazmulrobin.com\/blog\/refactoring-bloated-plugins-with-ai\/","title":{"rendered":"How I refactored two feature-rich but bloated plugins into good shape using AI"},"content":{"rendered":"\n<p>Two of my plugins on WordPress.org had the same problem. Each had gained features release after release, and each had ended up heavier than it should be, with parts that did not do what their label said. I rebuilt both with Claude Code. This post is the process I followed, with real numbers and findings from the two rebuilds.<\/p>\n<p>The plugins are <a href=\"https:\/\/wordpress.org\/plugins\/nhrrob-secure\/\">NHR Secure<\/a>, a security plugin, and <a href=\"https:\/\/wordpress.org\/plugins\/nhrrob-options-table-manager\/\">NHR Database Cleaner &amp; Optimizer<\/a>, which began as an options table manager. I wrote about using the same tool for core work in <a href=\"https:\/\/nazmulrobin.com\/blog\/contributing-to-wordpress-core-with-ai\/\">how I contribute to WordPress core with AI<\/a>.<\/p>\n<h2>What bloated looked like<\/h2>\n<p>Neither plugin was bloated because of one bad decision. Each feature had been added in the quickest way at the time.<\/p>\n<p>In NHR Secure 1.3.3:<\/p>\n<ul>\n<li>A full Tailwind CSS build of 11.7 KB was shipped for one section of the user profile page.<\/li>\n<li>A two-factor library was bundled, although a time-based code is one <code>hash_hmac<\/code> call. That library needed PHP 8.2 while the plugin said it supported 7.4.<\/li>\n<li>Settings were 22 separate autoloaded options, plus a custom database table for the activity log.<\/li>\n<li>One class of 500 lines held all 16 REST routes.<\/li>\n<li>There were three different pieces of code for finding the visitor&#8217;s IP address.<\/li>\n<\/ul>\n<p>In the database plugin, a new React interface had been added in 2.0.0 next to the old one. The old &#8220;Classic&#8221; screens were still there, with about 212 KB of assets of their own.<\/p>\n<p>A plain cleanup would have been the usual answer. What changed my plan was the audit.<\/p>\n<h2>Step 1: an audit that reproduces things<\/h2>\n<p>Before any code was written, I asked for an audit of the released version, with one rule: a finding is either reproduced on a test site or marked as read from the code. In the notes each finding carries a <strong>V<\/strong> for verified or a <strong>C<\/strong> for code.<\/p>\n<p>The audit of NHR Secure 1.3.3 listed 21 findings. Some of the verified ones:<\/p>\n<ul>\n<li><strong>Failed logins were counted twice.<\/strong> A helper created a second copy of a class on every request, and its constructor registered the &#8220;login failed&#8221; hook again. One wrong password counted as two, so a limit of five locked a user out after three.<\/li>\n<li><strong>The request filter blocked ordinary words.<\/strong> The pattern <code>\/.ini\/i<\/code> was meant for <code>.ini<\/code> files. With the dot not escaped it matched &#8220;training&#8221; and &#8220;administration&#8221;.<\/li>\n<li><strong>The malware scan flagged normal code.<\/strong> The pattern <code>\/system\\s*\\(\/i<\/code> matches <code>WP_Filesystem(<\/code>. On my test site it flagged 65 legitimate plugin files, each with a Delete button beside it.<\/li>\n<li><strong>The security score was out of 130 but graded as if out of 100.<\/strong><\/li>\n<\/ul>\n<p>None of these is a bloat problem. They are what feature-by-feature growth hides: each feature worked in the one case I tried when I wrote it.<\/p>\n<p>So the job changed from &#8220;make it lighter&#8221; to &#8220;make every feature do what its label says, and make it lighter&#8221;.<\/p>\n<h2>Step 2: write the rules down first<\/h2>\n<p>The audit became the first section of a product document that lives in the plugin&#8217;s repo, in a folder that is not shipped. The AI is told to read it before it proposes anything. It has four kinds of content.<\/p>\n<p><strong>Principles that cannot be traded away.<\/strong> For the security plugin the first one is:<\/p>\n<blockquote>\n<p>A security plugin must not make the site less safe or less reachable. It does what the label says, on a default server. Verified with a real request, not by reading the code.<\/p>\n<\/blockquote>\n<p><strong>Budgets with numbers.<\/strong> The zip may not grow past the last release. No runtime PHP dependencies. No JavaScript or CSS framework beyond what WordPress ships. A number can be checked after every feature. &#8220;Keep it light&#8221; cannot.<\/p>\n<p><strong>Constraints that protect existing users.<\/strong> The slug, the option prefix and the REST namespace never change, because renaming them breaks installs and buys nothing.<\/p>\n<p><strong>Decisions already taken.<\/strong> A section headed &#8220;Decided (don&#8217;t relitigate)&#8221;. Without it, every new session reopens the same questions, and an AI will happily argue both sides again.<\/p>\n<p>A shorter file, <code>CLAUDE.md<\/code>, holds the rules for writing code in that plugin. Two from the security plugin:<\/p>\n<blockquote>\n<p><strong>One client address:<\/strong> always <code>Core\\Ip::client()<\/code>. Never read <code>REMOTE_ADDR<\/code> or forwarded headers anywhere else.<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>Four options, no more.<\/strong> New features store into <code>state<\/code> or <code>scan<\/code>; they do not add an option.<\/p>\n<\/blockquote>\n<p>Each of those rules is there because the old code broke it.<\/p>\n<h2>Step 3: a mockup before the build<\/h2>\n<p>Both plugins got a new interface. Before it was built, I had the screens drawn as one HTML mockup and read it like a user would. The decisions that came out of that, such as the plugin&#8217;s name, its tags and its accent colour, went into the document. Only then did I say go ahead.<\/p>\n<p>Changing a mockup is cheap. Changing eight built screens is not.<\/p>\n<h2>Step 4: remove before adding<\/h2>\n<p>The rebuild started with deletions.<\/p>\n<p>In NHR Secure, the two-factor library went, replaced by the plugin&#8217;s own small TOTP class with unit tests against the standard&#8217;s test vectors. Tailwind and a select component went. The custom table was migrated into an option with a cap on rows and size, and dropped. The 22 options became one.<\/p>\n<p>In the database plugin, the Classic interface was removed, but only after every feature it had was confirmed to exist in the new one. Then the code that only the Classic screens had called was removed too.<\/p>\n<p>The measured results:<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Before<\/th>\n<th>After<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>NHR Secure, main admin script<\/td>\n<td>125 KB<\/td>\n<td>80 KB<\/td>\n<\/tr>\n<tr>\n<td>NHR Secure, main admin stylesheet<\/td>\n<td>38 KB<\/td>\n<td>20 KB<\/td>\n<\/tr>\n<tr>\n<td>Database plugin, zip<\/td>\n<td>280 KB<\/td>\n<td>261 KB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The database plugin&#8217;s zip got smaller in the same release that added a whole set of cleanup features, because removing the Classic interface freed more than the new features cost.<\/p>\n<h2>Step 5: a release gate, run on the final code<\/h2>\n<p>&#8220;It works on my machine&#8221; is where the old versions came from. Each rebuild ended with a list that had to pass on the final code, not on an earlier state of it:<\/p>\n<ul>\n<li>PHPCS, ESLint and the unit tests, including on PHP 7.4.<\/li>\n<li>Plugin Check on a production copy of the plugin.<\/li>\n<li>Semgrep and PHPStan.<\/li>\n<li>A probe that calls every REST route as an anonymous visitor and as a subscriber, and fails if either gets through.<\/li>\n<li>An upgrade from the released version on a site with real settings.<\/li>\n<li>For the security plugin, lockout drills: lose the login address, lose the second factor, block my own IP, and get back in each time.<\/li>\n<\/ul>\n<p>The upgrade test earned its place. In the database plugin it found that restoring an old snapshot of the options table also restored the <code>cron<\/code> option, which rewound every plugin&#8217;s schedule on the site. Snapshots no longer include that option.<\/p>\n<p>The probe is also tested. It is run once against a route with its permission check deliberately broken, to prove that it fails when it should.<\/p>\n<h2>Step 6: a second review by a different model<\/h2>\n<p>After the security plugin passed its gate, I asked for a full review of the branch by other models, one for security and one for performance and correctness, both read-only.<\/p>\n<p>They found 3 blockers and 21 major issues in code that had passed every check above. One blocker: two-factor codes could be guessed without limit, because each new sign-in started a new challenge with a fresh count of tries. Every finding was fixed and checked with a real request or a unit test, and the gate was run again.<\/p>\n<p>The model that wrote the code is the wrong one to review it. It agrees with itself.<\/p>\n<h2>What I kept for myself<\/h2>\n<ul>\n<li><strong>Scope.<\/strong> What the plugin is and is not. The security plugin&#8217;s document says it is not a web application firewall and not a malware cleaner, and its readme says the same.<\/li>\n<li><strong>Trade-offs.<\/strong> When a budget and a feature disagree, I choose.<\/li>\n<li><strong>The merge and the tag.<\/strong> The AI prepares a branch. I review it, merge it and release it.<\/li>\n<\/ul>\n<h2>Things that caught me<\/h2>\n<ul>\n<li><strong>I missed a budget, and wrote it down.<\/strong> The security plugin&#8217;s zip was meant to stay at 141 KB. The rebuilt version is larger, because one settings page became eight screens, a scanner and tests-backed rule sets. The document records the miss with the size of each part, so it stayed a decision and did not become a surprise.<\/li>\n<li><strong>&#8220;Done&#8221; needs evidence.<\/strong> The first principle says a protection is verified with a real request, not by reading the code. The audit is why: some features looked right in the code and did nothing on a real server.<\/li>\n<li><strong>Deviations happen and should be listed.<\/strong> In several places the build did something other than the plan, for good reasons. Each one is in the document under &#8220;Deviations from the plan&#8221;, with the reason.<\/li>\n<li><strong>A rule set needs a list of what it must not block.<\/strong> The rebuilt request filter has a unit test with 35 normal requests that must pass, next to 23 probes that must be caught. A pattern that only ever meets attacks looks perfect.<\/li>\n<li><strong>Old identifiers are forever.<\/strong> The database plugin&#8217;s slug still says &#8220;options table manager&#8221;, and both plugins keep their first prefixes. Existing installs depend on them, so they stay.<\/li>\n<\/ul>\n<p>The AI wrote the code in both rebuilds, and I reviewed and released it. What made the result good was the audit before it, the numbers it was held to, and the gate after it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two of my WordPress plugins had grown feature by feature until they were heavy and, in places, wrong. I rebuilt both with Claude Code. This is the process that made it work, from the audit to the release gate, and the parts I kept for myself.<\/p>\n","protected":false},"author":1,"featured_media":187,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_nhrrob_social_share_linkedin_text":"Two of my WordPress plugins had grown feature by feature until they were heavy, and in places did not do what their label said.\n\nThe audit found things I had shipped:\n\u2192 Failed logins counted twice, so a limit of five locked a user out after three.\n\u2192 A request filter meant for .ini files that blocked the words \"training\" and \"administration\".\n\u2192 A malware scan that flagged 65 normal plugin files, each with a Delete button beside it.\n\nI rebuilt both with Claude Code. The tool mattered less than the process:\n1. An audit where every finding is reproduced on a test site, or marked as only read from the code.\n2. The rules written down before any code.\n3. A mockup before the build.\n4. Remove before adding.\n5. A release gate, run on the final code.\n6. A second review by a different model.\n\nWhat I kept for myself: the scope, the trade-offs, and the merge and the tag.\n\nAnd one honest miss: the security plugin's zip was meant to stay at 141 KB, and it didn't. It's written down as a decision, not left as a surprise.\n\nThe post has each step, with real numbers from both rebuilds.\n\nHave you used AI on a refactor, and what did you keep for yourself?","_nhrrob_social_share_linkedin_layout":"","_nhrrob_social_share_x_text":"","_nhrrob_social_share_reddit":[],"_nhrrob_social_share_pro_texts":[],"_nhrrob_social_share_pro_x_texts":[],"footnotes":""},"categories":[7],"tags":[21,16],"class_list":["post-52","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-ai","tag-plugin-development"],"_links":{"self":[{"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":1,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/91"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/media\/187"}],"wp:attachment":[{"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nazmulrobin.com\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}