Back to Community

How to Fix Common 'eval()'d code' Errors in the Code Snippets Plugin

18 threads Sep 9, 2025 PluginCode snippets

Content

If you use the Code Snippets plugin, you might have encountered an error message in your logs or on your site that points to a file called snippet-ops.php and mentions eval()'d code. These errors can be confusing because they point to a core plugin file, but the root cause is almost always a problem within one of your custom snippets.

This guide will explain what these errors mean and provide step-by-step solutions to help you identify and fix the problematic code.

What Does the 'eval()'d code' Error Mean?

The Code Snippets plugin uses a PHP function called eval() to execute the custom code you write in your snippets. When an error occurs in your snippet's code, the PHP error message includes the path to this internal execution process, which is why you see a reference to snippet-ops.php and a line number after eval()'d code.

In short: The error is coming from your snippet, not from the plugin itself. The line number mentioned after eval()'d code is the line in your snippet where the error occurred.

Common Causes and Their Solutions

Based on community reports, here are the most frequent causes for these errors and how to resolve them.

1. Syntax Errors

Error Example: Parse error: syntax error, unexpected...
Cause: A typo or mistake in your PHP syntax, like a missing semicolon, parenthesis, or quote.
Solution: Carefully review your snippet for any syntax mistakes. Using a code editor with syntax highlighting can help spot these issues. Common fixes include ensuring all strings are quoted and all statements are properly terminated.

2. Undefined Variables, Constants, or Array Keys

Error Examples:
Undefined variable: $return_string
Undefined array key "content"
Undefined constant "Reload"
Cause: Your code is trying to use a variable, array key, or constant that has not been defined or does not exist.
Solution: Check for typos in variable names. For arrays, use the null coalescing operator (??) to provide a fallback value (e.g., echo $data['content'] ?? '';). Constants and strings in arrays must be quoted (e.g., ['Reload', 'Recharges']).

3. Trying to Use Properties on Null Objects

Error Example: Attempt to read property "ID" on null
Cause: This is a very common error after upgrading to PHP 8.0+, which is stricter. Your code is trying to access a property (like $post->ID) on a variable that is actually null.
Solution: You must check if the object exists before trying to use it. Wrap the code in a conditional check.
Example Fix:
// Before (causes error if $post is null):
if ( is_single( $post->ID ) )

// After (safe check):
if ( $post && is_single( $post->ID ) )

4. Type Errors

Error Example: Unsupported operand types: float + string
Cause: You are trying to perform an operation (like addition) on two values of different types (e.g., a number and a string). PHP 8.0+ is much stricter about this.
Solution: Ensure the values are of the correct type before using them. You can cast variables to a specific type.
Example Fix:
// Cast a variable to a float (number) before doing math:
$variable1 = (float) $some_string_value;

How to Find the Problematic Snippet

Since the error log points to a snippet, your first task is to find which one is causing the issue.

  1. Use the Search Feature: The Code Snippets plugin has a search function. Search for unique variable names, function names, or constants mentioned in the error message.
  2. The Disable-and-Reactivate Method: Deactivate all your snippets. If the error stops, reactivate them one by one until the error reappears. This will identify the culprit.
  3. Check Recent Changes: If the error started recently, check any snippets you recently added or modified.

What If I Can't Access My Site? Use Safe Mode

If a snippet causes a fatal error that breaks your site, you can use the plugin's Safe Mode. This feature temporarily deactivives all snippets, allowing you to log into your WordPress admin and fix the problem.

To enable Safe Mode, add the following to your site's wp-config.php file above the line that says /* That's all, stop editing! Happy publishing. */:

define( 'CODE_SNIPPETS_SAFE_MODE', true );

Once you have logged in and deactivated the problematic snippet, remember to remove this line from your wp-config.php file.

Conclusion

Errors containing eval()'d code are almost always a sign of an issue in your custom snippet code, not a bug in the Code Snippets plugin. The solutions typically involve reviewing your code for typos, adding proper checks for variables and objects, and ensuring type safety, especially after a PHP version update. By methodically identifying the problematic snippet and applying these common fixes, you can resolve these errors and keep your site running smoothly.

Related Support Threads Support