WP_Error is a WordPress feature used to simplify handling within WordPress Plugins or Themes. But what if we get an error message saying Call to undefined method WP_Error?
Usually, this error occurs because we are using the latest version of a plugin or theme, but our WordPress Core version is not yet updated.
How to Fix WordPress Call to undefined method WP_Error
When encountering this error, the website will display the message HTTP ERROR 500 and in the error_log file, we can find a message like the following
- Uncaught Error: Call to undefined method WP_Error::has_errors()
- Uncaught Error: Call to undefined method WP_Error::get_data()
- Uncaught Error: Call to undefined method WP_Error::addThemeDir()
- Uncaught Error: Call to undefined method WP_Error::get_id()
- And so on.
Although these error messages vary, the way to fix them can be the same, and we will discuss everything in this article.
Method 1: Update WordPress Core
If you are still using an old version of WordPress, especially a version below 5.9, and you update a theme or plugin to the latest version, you might get this error message.
One solution we can try is to update the WordPress version.
How do we update the WordPress version if we cannot even access the WordPress Dashboard page?
If the hosting or server you are using does not use a control panel that includes Softaculous, you can follow this method to update WordPress to the latest version:
Wait, we want to upgrade, not downgrade, right?
Don't worry, even though the method in that article is for downgrading, it can also be used to upgrade the WordPress version as long as the WordPress Core files uploaded are the latest version.
Method 2: Disable the Plugin or Theme Causing the Error
When an error occurs, the message in the error log usually does not just say Call to undefined method WP_Error, but there will be an additional message indicating the location of the code causing the error, for example “PHP Fatal error: Uncaught Error: Call to undefined method WP_Error::has_errors() in /home/user123/public_html/wp-content/themes/neve/functions.php:89“
Based on that message, the error is caused by the theme named Neve, and we can temporarily disable that theme using File Manager or FTP.
After the Theme or Plugin is disabled and we can log in, we can use an alternative theme or plugin, or fix the code that caused the error using the third method below.
Method 3: Delete or Disable the WP_Error Code
Yes, because this WP_Error code is actually causing the error, we just need to delete or disable the code that is causing it.
Because the error here is located in the file /home/user123/public_html/wp-content/themes/neve/functions.php:89, we can edit the file mentioned in the error log, and we will find lines of code that look something like this
Delete the code from start to finish, or we can comment out the code by adding a double slash (//) to each line of code until the last line, then save it, until the result looks like this
Method 4: Update the class-wp-error.php File
Because the error is caused by outdated code in WordPress, but the theme or plugin is adjusted to the latest WordPress code, we can update the class-wp-error.php file located in the WordPress installation folder, for example /public_html/wp-includes/class-wp-error.php
After the class-wp-error.php file is found, edit the file and update it using the following code
<?php
class WP_Error { public $errors = array();
public $error_data = array();
protected $additional_data = array();
public function __construct( $code = '', $message = '', $data = '' ) { if ( empty( $code ) ) { return; }
$this->add( $code, $message, $data ); }
public function get_error_codes() { if ( ! $this->has_errors() ) { return array(); }
return array_keys( $this->errors ); }
public function get_error_code() { $codes = $this->get_error_codes();
if ( empty( $codes ) ) { return ''; }
return $codes[0]; }
public function get_error_messages( $code = '' ) { // Return all messages if no code specified. if ( empty( $code ) ) { $all_messages = array(); foreach ( (array) $this->errors as $code => $messages ) { $all_messages = array_merge( $all_messages, $messages ); }
return $all_messages; }
if ( isset( $this->errors[ $code ] ) ) { return $this->errors[ $code ]; } else { return array(); } }
public function get_error_message( $code = '' ) { if ( empty( $code ) ) { $code = $this->get_error_code(); } $messages = $this->get_error_messages( $code ); if ( empty( $messages ) ) { return ''; } return $messages[0]; }
public function get_error_data( $code = '' ) { if ( empty( $code ) ) { $code = $this->get_error_code(); }
if ( isset( $this->error_data[ $code ] ) ) { return $this->error_data[ $code ]; } }
public function has_errors() { if ( ! empty( $this->errors ) ) { return true; } return false; }
public function add( $code, $message, $data = '' ) { $this->errors[ $code ][] = $message;
if ( ! empty( $data ) ) { $this->add_data( $data, $code ); }
do_action( 'wp_error_added', $code, $message, $data, $this ); }
public function add_data( $data, $code = '' ) { if ( empty( $code ) ) { $code = $this->get_error_code(); }
if ( isset( $this->error_data[ $code ] ) ) { $this->additional_data[ $code ][] = $this->error_data[ $code ]; }
$this->error_data[ $code ] = $data; }
public function get_all_error_data( $code = '' ) { if ( empty( $code ) ) { $code = $this->get_error_code(); }
$data = array();
if ( isset( $this->additional_data[ $code ] ) ) { $data = $this->additional_data[ $code ]; }
if ( isset( $this->error_data[ $code ] ) ) { $data[] = $this->error_data[ $code ]; }
return $data; }
public function remove( $code ) { unset( $this->errors[ $code ] ); unset( $this->error_data[ $code ] ); unset( $this->additional_data[ $code ] ); }
public function merge_from( WP_Error $error ) { static::copy_errors( $error, $this ); }
public function export_to( WP_Error $error ) { static::copy_errors( $this, $error ); }
protected static function copy_errors( WP_Error $from, WP_Error $to ) { foreach ( $from->get_error_codes() as $code ) { foreach ( $from->get_error_messages( $code ) as $error_message ) { $to->add( $code, $error_message ); }
foreach ( $from->get_all_error_data( $code ) as $data ) { $to->add_data( $data, $code ); } } }}5. Change PHP Version
Although the error is caused by outdated code, in some cases, changing the PHP version can resolve this issue.
This version change does not have to be an update, but could also be a downgrade.
And among the PHP versions that often cause errors are PHP version 8.1 and above, so if you are currently using PHP version 8.1 or higher, you can try updating to version 7.4.
Conclusion on How to Fix WordPress Call to undefined method WP_Error
Call to undefined method WP_Error is one of the errors usually found in old WordPress installations, so it is highly recommended to keep updating WordPress, including the core, themes, and plugins, so that our website can continue to run normally.
If you have tried all these methods but are still experiencing errors, please feel free to leave a comment on this page, and perhaps we can help.
COMMENTS
Loading...