If you create a snippet with a named function definition in it, then you shouldn’t duplicate and activate it without modification. Named functions can very easily cause collisions, and an exact duplicate will definitely cause an error.
The button style is purely for aesthetic purposes. In reality it’s still just a link to a HTTP URL which will perform the activation or deactivation. A simple button or link would perform exactly the same.
Additionally, direct database access is not required – there are multiple ways built into the plugin to temporarily prevent problematic snippets from executing to regain site access. You can even do this without editing any sites at all, as is explained in the pinned thread.
The solution here is twofold:
1. Don’t blindly duplicate and activate snippets without ensuring that they will not cause code errors.
2. Avoid using named functions wherever possible – in most cases, anonymous functions are a better choice.
setting define(‘CODE_SNIPPETS_SAFE_MODE’, true);
is easier than accessing database.
It would be however nice, if it would be possible to deactive a snippet, which was by accident activated.
I’m not really sure what you mean. How would the site know whether a snippet has been activated by accident?
An easier method than setting the constant is just appending &snippets-safe-mode=1 to the current URL, ideal for a quick fix.
In the single button, which has round corners, changing deactivate/activate can easily fail, why by accident an unwanted snippet is activated.
Ah okay, I see what you’re saying. How would you improve on the current design to make it a little more user-friendly?
I just don’t know. The current method looks good but it does have disadvantage.
Hi @tapiohuuhaa,
I thought a bit more about your suggestions and I think I’ve found what is hopefully a helpful solution. I’ve written a basic validator which essentially goes through the snippet code and looks to see if it defines any functions or classes which already exist.
Now when you click on an activate button it will run the code through this validator before doing the activation. If it fails, you’ll see an error icon instead of the snippet being activated.
Here’s a demo of how that looks:
View post on imgur.com