PHP Fatal error: Call to undefined method WP_Error::has_errors()

TLDR:

  • SSH onto your wordpress host
  • cd to the wp-includes directory
  • open a text editor (I used vim)
  • copy and paste the has_errors function code (see below for code snippet)
  • save the file
  • exit

Boring details and thought process while I did debugging:

A few months ago when I tried upgrading to wordpress I got this error message. Didn’t have enough time to investigate. Some websites were recommending manually installing the latest version of word press by copying over all the files. This seemed incredibly time consuming so I put it off until I had to renew my hosting and domain. Which was just right now.

Turns out the actual solution took just a few minutes of googling and SSHing onto the box to perform some surgery. My first attempt was to SSH onto my box and comment out the call to has_errors in wp-admin/file.php. Well, it got rid of that error, but then it errored out elsewhere in a different php file – same issue – has_errors did not exist.


So, I looked up the WP_Error::has_errors() code online by googling WP_Error class: https://developer.wordpress.org/reference/classes/wp_error/

In the excellent documentation which contains the entire source file, it states that it is located at wp-includes/class-wp-error.php. The specific code snippet is:

/*** Verify if the instance contains errors.
** @since 5.1.0
** @return bool
*/

public function has_errors() {
if (!empty($this->errors)) {
returntrue;
}
return false;
}

Next I sshed onto the box, went to wp-includes/class-wp-error.php, and opened with VIM. The code on the documentation showed that has_errors should have been defined on line 171. In VIM I hit esc to enter command mode, typed 171, and then hit Shift G to navigate there. I saw that has_errors was indeed missing. Looking at the online documentation, right before has_errors is get_error_data and after has_errors function is an add function. Comparing to my class-wp-error.php file, I saw the get_error_data and add functions, but has_errors was missing. So it was as though somehow that function got deleted during the ugprade. So I hit “Shift O” in command mode to insert a new line, and then copy pasted the code snippet.

Then I hit shift ZZ to save and exit out of VIM. After that I went back to wordpress and reattempted the upgrade and this time it went smoothly.

Leave a Reply

Your email address will not be published. Required fields are marked *