Did you run into a PHP error from one of your WPCode Snippet? Worry not. This article is going to guide you through the steps you need to take in order to find what is causing the issue so that you can try to fix it.
Is the error blocking your site?
First thing’s first: is the error that you ran into blocking your access to your website’s admin? If yes, please use the WPCode Safe Mode to access the admin and disable the faulty snippet until we find a fix for the error.
Enabling the WPCode Safe mode is as simple as adding ?wpcode-safe-mode=1
to the URL you are visiting. If your site’s admin URL is https://example.com/wp-admin
you should be able to access the WPCode Safe Mode by going to https://example.com/wp-admin/?wpcode-safe-mode=1
If Safe Mode is enabled you will see this notice which means you can now safely go to the list of Code Snippets and deactivate the snippet that is causing issues.
For more info about how the Safe Mode works and how snippets get automatically deactivated in WPCode please check out this article.
Finding the snippet that caused the issue
Starting with version 2.1.3 the automatic disabling of snippets has been improved and WPCode will more precisely deactivate just the snippet that caused the error and also highlight the line where the error occurred. You can easily see which snippets have been automatically deactivated by going to the list of snippets in the admin and looking for the “Automatically Deactivated” filter as shown below.
Once you click that you will see a list of just the snippets that have been deactivated and in the edit screen of the snippet you will see a notice with the details of when the snippet was deactivated and, if available, the line where the error was thrown which is also highlighted with red in the code editor as shown below.
Please note: in some specific situations the exact line of code might not be available or it might not match the exact line where you have to make a change to fix the snippet. This happens due to how PHP reports errors and in case that happens it’s a good idea to also look at the error logs to figure out how to fix the error.
If you are using an older version of WPCode (<2.1.3) please use the method described below to debug the issue:
As you will likely notice, due to the way the PHP code in the WPCode snippets is executed dynamically, there’s no reliable way for us to log which specific snippet caused the error you encountered. This also means that in some situations, multiple snippets will get automatically disabled until the error is no longer preventing you from accessing your site.
Finding the snippet when you have the error log is a matter of extracting the relevant information from the error log to point to the snippet. In the case of the log shown above, it’s mentioned that the snippet is calling a function ShowErrors
that is undefined. This makes it easy to find the snippet by using the search box in the list of snippets and searching for ShowErrors
.
The snippet search in the admin area is optimized for finding code in this way and it should be easy to trace down the snippet causing issues.
Once you found the snippet causing the error you should update the code to try to prevent the error. In this case you could add a check to see if the function_exists before attempting to call the function.
Using the Error Log
If your snippet was automatically disabled because of an error, if you just used the Safe Mode to deactivate the snippet or if the snippet is throwing an error in the frontend of your site the first thing you need to do in order to fix the error is to figure out exactly what error you are running into.
In order to do that, WPCode offers a convenient way to see error messages related to snippets directly in the admin in the Error Logging area. If you don’t already have error logging enabled, you can enable it from the WPCode Settings page:
Please note: we recommend disabling error logging and deleting the logs when you finish debugging.
Now that error logging is enabled all that’s left is to trigger the error again. That’s right. Because the error logging was not previously enabled the error is likely only registered in the PHP server logs so there’s no easy way for you to access it. Triggering the error again with the WPCode Error Logging enabled means the error will be registered in the WPCode error logs and you’ll be able to see it in the admin easily.
If you already had error logging enabled or if you just triggered the error again by activating the snippet one more time after error logging was enabled you should now be able to see the error thrown in the WPCode Error Logs under Code Snippets > Tools > Logs:
Important: Don’t forget to select the most recent error log from the top-right dropdown – by default the oldest log is loaded first and you should delete logs that are no longer relevant.
If you need further assistance in preventing your snippet from throwing an error don’t hesitate to reach out and we’ll try to help you find a solution.