Fixing the 'Call to undefined function create_function()' Error in WordPress

intermediateπŸ“ WordPress2026-06-16| WordPress environments running PHP 8.0, 8.1, 8.2+ on Linux (Ubuntu/CentOS), Windows (XAMPP/WAMP), or managed hosting.

Error Message

Fatal error: Uncaught Error: Call to undefined function create_function()
#php-8#wordpress-fix#coding-standards#troubleshooting

Why Your Site Crashed

Upgrading to PHP 8.0 or higher is great for performance, but it’s a breaking change for older code. If you see a white screen or a fatal error log, your site likely relies on a legacy function called create_function(). This function was officially removed in PHP 8.0.

Fatal error: Uncaught Error: Call to undefined function create_function() in /path/to/wp-content/plugins/old-plugin/filename.php:line_number

The Root Cause

PHP 7.2 deprecated create_function() back in 2017. It was slow and insecure because it internally used eval(), which allowed execution of arbitrary code strings. Because it was a security risk, PHP 8.0 finally deleted it from the engine. If a plugin or theme written five or six years ago is still active on your site, it will crash the moment you toggle that PHP 8 switch.

Step 1: Locate the Broken Code

You need to find the specific file causing the conflict. If your browser only shows a generic "Critical Error" message, you'll need to check your error logs or enable WordPress debugging. This reveals the "smoking gun."

Open your wp-config.php file and update these lines:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );

Refresh your site. The error will now point directly to a file path, usually inside wp-content/plugins/ or wp-content/themes/. Take note of the file name and the line number mentioned.

Step 2: Update Everything

Before you touch any code, check for updates. Most reputable developers patched this years ago. If you are using a version of a popular plugin like Slider Revolution or LayerSlider from 2018, simply updating to the latest version will solve the problem instantly.

  • Premium tools: Log into the marketplace (like ThemeForest) to download the latest PHP 8-compatible files.
  • Abandoned plugins: If a plugin hasn't been updated in 3+ years, it's a security liability. Switch to a modern alternative rather than trying to patch it.

Step 3: Manually Refactor to Anonymous Functions

If you're stuck with custom code that you can't replace, you must rewrite the function. You need to convert the old string-based create_function into a modern PHP anonymous function (also called a closure).

Example 1: Simple Filter Logic

Old Code (Broken):

add_filter('excerpt_length', create_function('$a', 'return 25;'));

New Code (Fixed):

add_filter('excerpt_length', function($a) {
    return 25;
});

Example 2: Complex Logic with Arguments

Old Code (Broken):

$new_func = create_function('$val', 'return my_custom_logic($val) . "_suffix";');
add_filter('some_hook', $new_func);

New Code (Fixed):

$new_func = function($val) {
    return my_custom_logic($val) . "_suffix";
};
add_filter('some_hook', $new_func);

Example 3: Passing Variables with 'use'

The trickiest part of create_function was how it handled external variables. In modern PHP, we use the use keyword to bring those variables into the function's scope.

Old Code (Broken):

$suffix = '_test';
add_filter('wp_title', create_function('$title', 'return $title . "' . $suffix . '";'));

New Code (Fixed):

$suffix = '_test';
add_filter('wp_title', function($title) use ($suffix) {
    return $title . $suffix;
});

Step 4: Verify and Clean Up

After saving your changes, perform a quick health check:

  • Flush Cache: Clear your server cache and any plugins like WP Rocket.
  • Check Site Health: Go to Tools > Site Health in your dashboard to ensure no other PHP errors are lingering.
  • Toggle Debug Off: Once the site is stable, set WP_DEBUG back to false in your wp-config.php to keep your logs from bloating.

Future-Proofing Your Site

Don't get caught off guard by the next PHP update. Use a staging environment to test major version jumps (like moving from 8.1 to 8.2). If you see "Deprecated" notices in your logs, address them immediately. These warnings are early indicators that a function will be removed in the next major release, giving you plenty of time to refactor without the stress of a crashed site.

Related Error Notes