Documentation, Reference Materials, and Tutorials for WPCode
Would you like to add PHP snippets to your site without worrying that one mistake will lock you out of your WordPress admin? All you need is WPCode and a PHP snippet you’re working on.
In this guide, we’ll cover WPCode’s checks for PHP snippets, what happens when one fails, and how to use safe mode.
Before you get started, make sure WPCode is installed and activated on your WordPress site.
To get started, we’ll look at the check WPCode runs when you switch a snippet on. WPCode protects your site in two layers. This is the first one.
When you activate a PHP or Universal snippet, WPCode runs the code once in a protected space. It only activates the snippet if no error comes up.
For this example, we’re using a PHP snippet that calls a function named get_store_hours_label(). That function isn’t defined on this site. In our example, it comes from a store-hours plugin that isn’t installed here.
Because the snippet calls that function right away, the activation check catches the error.
If you’re new to building snippets, see our guide on creating your first snippet.
Once your code is in place, switch the toggle at the top of the editor to Active.

Next, click Save Snippet.

WPCode saves your changes but leaves the snippet switched off. A red message names the error it found, so you know exactly what to correct before you try again.

The same check runs when you switch a snippet on from the Code Snippets list.
Some code passes the activation check and only fails later. A common case is code that waits for a WordPress hook, such as admin_menu, and only runs its failing line when that hook fires. This is where WPCode’s second layer comes in.
In our example, we moved the get_store_hours_label() call inside the admin_menu hook and set the snippet’s location to Admin Only. That version passes the activation check, because the hook doesn’t fire while WPCode runs it.
WPCode watches every snippet it runs in the Run Everywhere and Admin Only locations. Snippets in these two locations run on every admin page. So if one of them triggers a fatal error while your admin area loads, WPCode switches that snippet off straight away.
WordPress then shows a short error screen for that one page load. The message on it confirms WPCode has already deactivated the snippet. Your admin area works normally again from the next page you open.
To see which snippet it was, click View Snippets With Errors.

Next to it, the View error logs button opens WPCode’s error logs. That button appears when error logging is turned on. Otherwise, the screen offers an Enable error logging button that opens WPCode’s settings.
Our guide to snippet error logging covers both.
Note: WPCode switches a snippet off automatically only when it runs every time your admin area loads. For the full list of locations and when each one runs, see our guide to snippet locations.
You don’t need the error screen to find the snippet again later. When a snippet has thrown an error, the WPCode item in the WordPress admin bar shows a count. This item comes from WPCode’s Admin Bar Info setting, which is on by default.
Hover over the WPCode item. Then click Snippets With Errors.

Either route opens a view of the Code Snippets list that shows only the snippets that threw errors. A note at the top explains what the view contains.
A red icon beside the status toggle marks each of these snippets. Hover over the icon to see the time and date WPCode switched the snippet off. To see the full details, click the snippet’s name.

The snippet opens in the editor, with everything you need to fix it.
When you open the snippet, a message at the top of the editor explains what happened. It shows when WPCode deactivated the snippet and the error message itself.
The message also tells you which line of your code caused the error. WPCode highlights that line in the code editor.

For more detail, the message points you to the error log. With error logging turned on, WPCode writes each error to its main log and to a log file for that snippet. That file’s name starts with snippet- followed by the snippet’s ID.
Next, correct the code. In our example, the fix is to check that the function exists before calling it.

When your code is ready, switch the toggle back to Active.

Then click Update.

WPCode runs its activation check on the corrected code. If it passes, you’ll see a Snippet updated message.

Your snippet is active again, and the error message from before is gone.
Safe mode lets you pause all of your snippets at once. It’s a fallback for when you’d like to find out which snippet is behind something unexpected on your site. That works even if the snippet hasn’t thrown an error.
While it’s on, WPCode doesn’t run any of your auto-inserted snippets.
To turn it on, add &wpcode-safe-mode=1 to the end of your Code Snippets page address. For example, if your site is at https://example.com, visit https://example.com/wp-admin/admin.php?page=wpcode&wpcode-safe-mode=1.
Safe mode works on any admin address. If the address has no question mark in it, such as https://example.com/wp-admin/, add ?wpcode-safe-mode=1 instead.
A yellow notice at the top of the page confirms safe mode is on. WPCode adds the parameter to every admin link, so safe mode stays on as you move between pages.
Next, find the snippet you’d like to switch off in the Code Snippets list. Then turn off its status toggle.

Once you’ve switched off the snippet, click Exit safe mode in the notice.

The button opens the same page in a new browser tab without safe mode, so your snippets run again there. Your original tab stays in safe mode. If something still isn’t right, you can go back to that tab and switch off another snippet.
Safe mode only affects logged-in users who can activate snippets, so your visitors don’t see any change.
The safe mode parameter only works once you’re logged in. If a snippet stops you from logging in, you can turn on safe mode from your site’s configuration file instead.
The constant goes in wp-config.php, in the root folder of your WordPress site. It belongs above the line that starts with /* That's all, stop editing!, and it looks like this:
define( 'WPCODE_SAFE_MODE', true );
If you haven’t edited this file before, see How to Edit wp-config.php File in WordPress for a step-by-step guide.
While the line is in place, WPCode doesn’t run auto-inserted snippets for anyone who visits your site, not just for you. Log in, switch off the snippet that caused the problem, and then remove the line so your other snippets run again.
That’s it! Now you know how WPCode checks your PHP snippets, switches off one that fails, and gives you safe mode as a fallback.
Next, would you like to see the full details of every error your snippets report? Check out our guide on using snippet error logging.
Below, we’ve answered some of the most common questions about how WPCode handles PHP snippet errors.
WPCode runs the activation check on PHP and Universal snippets, because those are the two types that run PHP code. Other types, such as HTML, CSS, and JavaScript, activate without the check. For more on each type, see our guide on which type of snippet you need.
WPCode records the error and marks the snippet with the error icon in your Code Snippets list, so it shows up under Snippets With Errors. It doesn’t switch the snippet off automatically, because an error on the front end doesn’t stop you from reaching your admin area. To review it, open the snippet from that list. Then fix the code or switch the snippet off yourself.
No. Automatic deactivation covers snippets in the Run Everywhere and Admin Only locations, since both run whenever your admin area loads. Snippets in other locations still get the activation check when you switch them on.
No. WPCode switches the snippet off during the same page load that triggered the error. The next page you open loads normally.
Not with the safe mode parameter, which only works for logged-in users who can activate snippets. If you can’t log in, turn on safe mode from your site’s configuration file instead, as shown in Turning On Safe Mode When You Can’t Log In.
Yes. With a paid WPCode license, you can turn on email notifications for when a snippet throws an error or is automatically deactivated. Our guide to using snippet error logging shows how to set them up.
Future-proof your website with WPCode Snippets and improve the way you manage code across all your websites.