Cookie-based snippets temporary switch-off
-
Hello again,
when reading your support forum I stumbled upon a topic (closed already 3 years ago) under the motto “Oh my God, after the recent change my super-snippet brought my website down and now I cannot even login as an administrator to fix it!”.
What you propose is to temporarily set a constant CODE_SNIPPETS_SAFE_MODE=true in wp-config.php file. Personally, I don’t think it’s a good solution. Firstly, fiddling with wp-config is generally not the best idea ever. Secondly, if someone already has FTP access to his/her website, there is no need to edit the master configuration file: it’s enough to rename the wp-content/plugins/code-snippets directory to anything else, and the plugin will be immediately switched off by WordPress. This works for any plugin, and code-snippets is not an exception.
Instead, I have implemented a cookie-based temporary switching off mechanism on my own website. It requires just a few lines of code.
Firstly, I have stored the following file into the WP root directory:
<?php // file code-snippets-off.php
$seconds = 180;
setcookie( "code-snippets-off", "true", time()+$seconds, "/", $_SERVER['HTTP_HOST'] );
echo "Code snippets inactive for {$seconds} seconds";
?>
Next, I have changed just one line of code in your snippet-ops.php file (first line of the execute_active_snippets function): instead of
if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {return false;}
I put
if ( isset($_COOKIE['code-snippets-off']) ) {return false;}
I think it’s pretty obvious how it works: it’s enough to type e.g. “https://my-website/code-snippets-off.php” in the address box of a web browser to get a 180-second cookie named “code-snippets-off” in the current browser session. This in turn suspends snippets execution.
I think 3 minutes should be perfectly enough to login to a website and deactivate either the whole Code Snippets plugin or just the troubled snippet. Also, the solution requires no FTP access (to be precise, one needs FTP access to set up the whole mechanism, but not to disable the snippets). This can be done even from a smartphone: everybody has a web browser on a smartphone, but few people have an FTP client installed (I don’t!).
This is also safe: the cookie disables the snippets just for your browser session, so the villains cannot paralyse the website e.g. by constantly calling code-snippets-off.php from the outside. Well, if someone needs an additional security measure, the cookie-setting file can be renamed to anything crazy-and-hard-to-guess. But then you’d better bookmark it in your browser, because you can also forget this crazy name when in panic…
Cheers — Jarek
- The topic ‘Cookie-based snippets temporary switch-off’ is closed to new replies.