How to Log PHP Errors in WordPress

How to Log PHP Errors in WordPress

There are many categories of WordPress errors that can cause problems with your blog. Ranging from database access, to permissions issues, files not found situations and PHP errors. Out of these, PHP errors can be the most frustrating to find and debug. Especially when your site simply stops working without giving you any advance warning! Before learning about the sophisticated error logging systems of WordPress, I used to go to the error file generated by my shared hosting provider where I had to isolate my particular account amongst all the others residing on the server.

If only I had known about how easy it was to configure PHP reporting years ago, I could have saved myself a lot of headache. There are many options and settings you can use to find tune PHP error logging. But let’s get started with a quick and simple method consisting of the following:

  1. Creating a log file;
  2. Telling WordPress to log errors into it;
  3. Securing the log file access.

Creating the PHP Error Log File

The first step is to create a log file with any name of your choosing – preferably with the extension “.log” to make identification easier. Many best practices recommend placing it in a location that is inaccessible to the general public – such as a protected folder or one directory above the “public_html” of your site. However, this may not be possible for you depending on what kind of hosting setup and permissions you have. So for this example, I’m simply going to create a file called “error_php.log” in the home directory of my WordPress blog.

create log file

I’ll worry about securing it in step three. It’s important to make note of the absolute path of the file you just created. This might not be easily visible to you depending on how you create the file and your FTP program. I personally use WinSCP, but I’m sure that you can get the absolute path if you right-click the file and look at its properties. In the screenshot above, you can see that I’ve circled the necessary information for use in the next step.

Configuring wp-config.php to Write Errors

Next, open up your wp-config.php file and scroll down towards the bottom where you see a line saying something like this:

/* That's all, stop editing! Happy blogging. */

Once you find it, copy the following code and paste it above the line indicated:

@ini_set( 'log_errors', 'On' );
@ini_set( 'display_errors', 'Off' );
@ini_set( 'error_log', '/home/webshed08/public_html/error_php.log' );

In the third line, replace the absolute file path with whatever you noted down when you first created the file. You need to make sure that you get this step absolutely right for error logging to work. Save your wp-config.php file and you should be set!

place code in wp-config

If you don’t have access to modify wp-config.php, try changing the permissions. I’d written an earlier article on securing wp-config.php with permissions.

Testing it Out

To make sure that error logging is working as promised, I went into one of my plug-ins and deliberately typed some nonsense into the PHP file.

create fake error

When my site broke as expected, I headed over to “error_php.log” to see if anything had happened. And sure enough, I got the expected error log:

error logged

So it works! We’re almost done – now we only need to secure the log file from prying eyes so that only we can access it. As of now, anyone can simply browse to the location of our file and view it, thereby getting valuable information about the internals of our WordPress installation. Securing it takes two steps to be sure:

Securing the Log File

First, go to the new log file properties via the FTP program and set the following permissions:

600 or 0600 (depending on whether you have to enter a three or four digit number)

set log file permissions

This will allow only the owner to read it. Second, open up your .htaccess file and type in the following:

<Files error_php.log>
Order allow,deny
Deny from all
</Files>

Once again, remember to replace “error_php.log” with your own file name. These two precautions taken together should ensure that your log file is nice and secure. You can always access it via an FTP program if necessary.

With this technique, never again will you have to worry about finding out what went wrong on your site. You always know where to find detailed PHP error logs.

One Reply to “How to Log PHP Errors in WordPress”!

  1. Very interesting article, I really admire your knowledge on the topic. I think our latest blog post might be of interest to you as it also discusses the same subject of solutions for the most common errors in WordPress. https://goo.gl/PudUAP Having your feedback regarding the article would be an honour for us.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.