You change a few lines in a snippet, press Save, and get “Could not update snippet. Request failed with status code 403”. Press Save on the unchanged snippet and it works. Your login is fine. Something between you and WordPress is reading the code itself.
Saves? Login and role are fine: the new code is what is refused.
Only the changed lines matter. One holds the pattern it matched.
Use an equivalent it leaves alone. Save, then read the code back.
The lasting fix is the host's: exclude the matching rule for the plugin's save path only.
Table of Contents
The symptom: a Code Snippets 403 that depends on what you typed
The Code Snippets plugin saves through the WordPress REST API, so the failure looks the same in two places:
- In wp-admin, the editor shows “Could not update snippet. Request failed with status code 403”.
- Over REST, a
POSTto the snippet’s route answers403 Forbidden, often with a plain host error page instead of WordPress’s usual JSON.
That second detail is the first clue. A refusal from WordPress itself comes back as JSON with a code such as rest_forbidden. A bare page in the host’s own style means the request never reached WordPress.
On the test site this happened on a WordPress multisite behind a managed host. Over a week, five different snippet changes were refused. Each one was found and fixed with the steps below.
1. Re-save the unchanged snippet
Open the snippet, undo your edit (or reload the editor without saving), and press Save.
- It saves: your account, its permissions and the plugin are all fine. The only difference between the request that worked and the one that failed is the code in its body. Go to step 2.
- It fails too: this is not the firewall’s content rules. Check that you are still signed in, that your role can edit snippets, and whether a security plugin is blocking the REST API (a 401 from the REST API is a different problem with its own checks).
On the test site the unchanged code saved every time, and the changed code was refused every time, three tries in a row. Nothing else was different.
If the unchanged code saves and the changed code doesn’t, the code is what is being judged.
2. Compare with the last version that saved
The firewall doesn’t say which rule matched. You find it by difference: put the refused version next to the last one that saved and look only at the lines that changed. On the test site every refusal came down to one of these:
| What the new code contained | Refused | The rewrite that saved |
|---|---|---|
| PHP’s globals array, used twice to pass a flag between functions | 2026-10-11 | a function holding a static variable |
| one call to a regular-expression split | 2026-10-15 | strtok() over the same text |
| a comment that named the PHP open tag and an HTML page-head tag, written as tags | 2026-10-18 | the same comment in words (“the PHP open tag”) |
| a style block printed straight from PHP | 2026-10-11 (likely) | the same CSS through wp_add_inline_style() |
| a raw SQL query reading a server variable | 2026-10-18 (suspected) | the database layer’s own helper method |
Two things in that list surprise people. Comments count: the firewall reads the raw text of the request, so a tag written inside a comment is matched like any other. And the request arrays count: reading form input or the query string directly through PHP’s built-in request arrays looks, to the firewall, like code that handles untrusted input.
The firewall reads the raw text of the request, comments included.
3. Rewrite the line with an equivalent the firewall leaves alone
Each pattern has a plain WordPress or PHP equivalent. None of them changes what the snippet does.
Pass state between functions with a static variable, not the globals array:
function myprefix_flag( $set = null ) {
static $flag = false;
if ( null !== $set ) {
$flag = $set;
}
return $flag;
}
Code language: PHP (php)
Read the query string with filter_input(), not the request arrays:
$round = (string) filter_input( INPUT_GET, 'round', FILTER_UNSAFE_RAW );Code language: PHP (php)
Split text with string functions where a regular expression isn’t needed:
$line = strtok( $text, "\r\n" );
while ( false !== $line ) {
// handle one line
$line = strtok( "\r\n" );
}
Code language: PHP (php)
Add CSS with wp_add_inline_style(), and let WordPress print the tags:
wp_register_style( 'myprefix', false, array(), '1' );
wp_enqueue_style( 'myprefix' );
wp_add_inline_style( 'myprefix', '.myprefix-box { padding: 8px; }' );
Code language: PHP (php)
Build page markup in JavaScript with DOM calls, so neither the PHP nor its output holds a literal tag. The script itself goes in with wp_add_inline_script() on a handle registered without a file:
var box = document.createElement('div');
box.className = 'myprefix-box';
box.textContent = 'Saved';
container.appendChild(box);
Code language: JavaScript (javascript)
Use the database layer’s helpers ($wpdb->get_col_charset(), $wpdb->prepare() with the table helpers) instead of writing a raw query into the snippet.
After each rewrite, save, then read the code back. A save that answered 200 but stored something else has happened before. On the test site the plugin’s REST route returns the stored code, and comparing it with what you sent byte for byte settles it.
Write the snippet to the firewall’s rules from the start, and it saves on the first try.
4. Write new snippets to these rules from the start
Once the rules are known, they cost almost nothing to follow. On 2026-11-19 a new snippet of about 800 lines went onto the same host in one save. It registers REST routes, builds a whole review page in JavaScript and adds its CSS inline. It was written with no globals array, no request arrays, no regular-expression calls, no raw SQL and no literal tag anywhere, comments included. It read back identical.
Before saving a large change, search it for those five things. Ten seconds of searching beats ten minutes of bisecting a refused save.
5. Or ask the host to exclude the rule
The rewrites are workarounds for a false positive. The lasting fix belongs to the host. Send them:
- the time of a refused save and the request path, which for this plugin is under
/wp-json/code-snippets/(and/wp-admin/if you save there); - a request to find the matching rule in the firewall log and exclude it for those paths only, rather than switching the firewall off.
Many managed hosts run rule sets such as the OWASP Core Rule Set and can exclude one rule for one path. Some will instead pause the firewall for a few minutes while you save. That works, but you’ll need it again for the next change.
When to rewrite and when to ask the host
- One refused save, an obvious trigger in the diff: rewrite the line.
- The trigger is the point of the code (a snippet that must handle raw SQL, or print a tag the browser needs): ask the host for an exclusion.
- Several people edit snippets on the site: ask the host. Not everyone will remember the rules.
- Saves fail even unchanged: stop here. It isn’t the firewall’s content rules (step 1).
The short version
A 403 that appears only when a snippet’s code changes comes from the host’s firewall reading the request. Re-save the unchanged snippet to prove it, compare with the last version that saved to find the line, and rewrite it: a static variable for globals, filter_input() for request data, string functions for simple splits, wp_add_inline_style() for CSS, DOM calls for markup, the database helpers for SQL. Read the code back after every save. For a lasting fix, ask the host to exclude the rule for the plugin’s save path.
Frequently Asked Questions
Does this happen with other code snippet plugins?
Yes, with any plugin that saves PHP through a form or a REST request: the firewall sees the same request body whichever plugin sends it. The steps are the same. Only the save path you give the host changes.
Is my code unsafe if the firewall flags it?
Not because of the flag. These rules match patterns that attackers also use, so ordinary code that reads input or prints markup trips them. If you choose a snippet over a plugin, the review of what the code does is still yours.
Can I avoid the firewall by uploading the code as a file?
A must-use plugin uploaded over SFTP never passes through the web request the firewall inspects, so it saves. You lose the snippet plugin’s editor, its on/off switch and its safe mode, and every change needs a file upload. For a one-time change, a short-lived snippet is often the better trade.