WordPress Uncaught Error: Call to undefined function create_function() solution

Have you also beenWordpresserror message"Uncaught Error: Call to undefined function create_function() in SNIPPET:62"Giving you a headache?

This problem is actually not difficult to solve. Let's talk in detail about why this problem occurs and how to solve it with simple and effective code.

Why do I get a create_function error?

Let's start with some background. In PHP 7.2,create_function()It has been deprecated, and in PHP 8.0, this function has been directly removed. In other words, if your server has been upgraded to PHP 7.2 or above and is still using the old code, this problem will occur.

But the core of the problem is:create_functionis a product of an old era. Its problem is not only that it is no longer supported, but also brings poor performance and potential security risks. The new solution is to useanonymous function(Anonymous Functions).

Modify the code: solve the create_function error

WordPress Uncaught Error: Call to undefined function create_function() solution

Now, let's see how to replace the old code. Here are the specific code modification plans:

original code

//彻底禁止WordPress缩略图
add_filter( 'add_image_size', create_function( '', 'return 1;' ) );

//自定义登录页面的LOGO链接为首页链接
add_filter('login_headerurl', create_function(false,"return get_bloginfo('url');"));

Modified code

// 彻底禁止 WordPress 缩略图
add_filter( 'add_image_size', function() { return 1; } );

// 自定义登录页面的 LOGO 链接为首页链接
add_filter( 'login_headerurl', function() { return get_bloginfo('url'); } );

Code explanation :

  • function() { return 1; } is an anonymous function that takes no parameters and returns 1.
  • get_bloginfo('url') It is a function that returns the homepage address of the website. We also wrap it with an anonymous function.

Better Practice: Be Careful When Disabling Thumbnails

Disabling WordPress thumbnails completely is not always the best option. If certain themes or plugins require images to be of a specific size, disabling it completely can cause unexpected issues.

Here's a more flexible solution: selectively disable unnecessary image sizes via filters.

Sample code

add_filter( 'intermediate_image_sizes_advanced', function( $sizes ) {
    unset( $sizes['thumbnail'] ); // 禁用缩略图
    unset( $sizes['medium'] );    // 禁用中等大小
    unset( $sizes['large'] );     // 禁用大图
    return $sizes;
} );

This approach not only solves the problem, but also maintains compatibility with certain functions.

Modify the code

Add the modified code to one of the following locations:

  1. Current topic functions.php file.
  2. Custom function plugins (recommended method to avoid code overwriting by theme updates).

What should I do if an error still occurs after modifying the code?

If the fluent-snippet-storage plugin is enabled and there are still errors after modification, it may befluent-snippet-storage/index.phpThere is a cache in the file, and the error code of error_files needs to be deleted.

turn upwp-content/fluent-snippet-storage/index.phpFile, go to the very end and find code similar to the following:

'error_files' => 
array (
'1-e7a7bbe999a4wordpresse5a4b4e983a8e697a0e794a8e4bfa1e681af.php' => 'Uncaught Error: Call to undefined function create_function() in SNIPPET:62',
),
);

Delete the code for these error records:

'1-e7a7bbe999a4wordpresse5a4b4e983a8e697a0e794a8e4bfa1e681af.php' => 'Uncaught Error: Call to undefined function create_function() in SNIPPET:62',

Bonus Tip: Ensure code compatibility

Before modifying the code, it is recommended to back up your website files and database. You can use plugins (such as UpdraftPlus) to quickly complete the backup to avoid being caught off guard when errors occur.

At the same time, if you are not familiar with PHP, it is recommended to try to modify the code in a test environment instead of directly operating the production environment.

Optimizing code is a constant battle

This problem seems simple, but it is actually a common problem that needs to be faced in website development:Compatibility of old code with new environmentWhether it is the upgrade of PHP version or the improvement of WordPress functions, it is to make the website run more efficiently and securely.

For developers, it is essential to update the code and learn new technologies in a timely manner. Anonymous functions can not only make the code more concise, but also improve performance and reduce potential security risks. Every optimization will make your website one step further.

Final Thoughts

  1. create_function() This is no longer supported in PHP 7.2 and above. It is recommended to use anonymous functions instead.
  2. After modifying the code, your website will be more efficient and secure.
  3. For better compatibility with plugins and themes, it may be a better solution to selectively disable image sizes.
  4. Optimizing code is not just about fixing problems, it is also a step to improve the quality of your website.

If you have encountered similar problems, you may want to try the solutions in this article.

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top