In the process of creating a mirror image of a current WordPress to be used for development purposes I ran into a PHP error that I had not seen before. The current version of WordPress was working without issue but because many of the settings needed to be modified for the installation to work with a different domain pointed to it. Even after modifying the settings that I had modified in previous instances to make WordPress work with an updated domain name I was still getting an error related to the WordPress Theme. I enabled some PHP error logging, started looking for answers on the web, and then implemented some changes described below to resolve the error.
PHP Code Modified In theme.php To Resolve PHP Error:
The below is specifically what was showing up in my PHP error log. You can enable PHP error logging by modifying your php.ini file which is typically located in the /etc directory. From a default PHP installation you typically only need to set error_logging to “On” and specify a log file with the error_log variable in php.ini. Once you set the location of the file which should be something like /var/log/phperror.log make sure that Apache or your web server can write to the file.
PHP Error: Fatal error: Call to a member function read()
Modifying the theme.php file by adding the below “if” statement will resolve the error.
PHP IF Statement To Resolve Member Function Error:
- if ( is_object($template_subdir) ) {
The statement will be added to line 387 of theme.php which is located in the “wordpress-root/wp-includes” directory. It will also need to be closed so below we show the original code followed by the updated code with the above IF statement added and also closed with a bracket on line 395.
Original theme.php PHP Code Chunk:
- if ( $template_dir ) {
- while ( ($file = $template_dir->read()) !== false ) {
- if ( preg_match('|^\.+$|', $file) )
- continue;
- if ( preg_match('|\.php$|', $file) ) {
- $template_files[] = "$theme_loc/$template/$file";
- } elseif ( is_dir("$theme_root/$template/$file") ) {
- $template_subdir = @ dir("$theme_root/$template/$file");
- while ( ($subfile = $template_subdir->read()) !== false ) {
- if ( preg_match('|^\.+$|', $subfile) )
- continue;
- if ( preg_match('|\.php$|', $subfile) )
- $template_files[] = "$theme_loc/$template/$file/$subfile";
- }
- @ $template_subdir->close();
- }
- }
- @ $template_dir->close();
- }
Updated theme.php PHP Code Chunk:
- if ( $template_dir ) {
- while ( ($file = $template_dir->read()) !== false ) {
- if ( preg_match('|^\.+$|', $file) )
- continue;
- if ( preg_match('|\.php$|', $file) ) {
- $template_files[] = "$theme_loc/$template/$file";
- } elseif ( is_dir("$theme_root/$template/$file") ) {
- $template_subdir = @ dir("$theme_root/$template/$file");
- if ( is_object($template_subdir) ) {
- while ( ($subfile = $template_subdir->read()) !== false ) {
- if ( preg_match('|^\.+$|', $subfile) )
- continue;
- if ( preg_match('|\.php$|', $subfile) )
- $template_files[] = "$theme_loc/$template/$file/$subfile";
- }
- @ $template_subdir->close();
- }
- }
- }
- @ $template_dir->close();
- }
Make sure that you add the code addition in the correct places or just update the entire bit of code. Before you actually update the code you shoudl backup the original theme.php file in case you run into any issues. This should resolve the “Fatal error: Call to a member function read()” error you were getting in relation to a WordPress Theme.