Diagnosing problems in WordPress with trailing whitespace

I recently worked on a project for a client which involved making a few improvements to their WordPress website and a simple website update. No problem I thought, one day’s work for the custom coding and probably 10 minutes for the update… How wrong I was!!!

After making my code updates to functions.php and upgrading WordPress to the latest version everything worked correctly… that is until I tried to attach an image to a post and as you can see by the following screenshot, no posts are available for me, ouch!

To diagnose this you can do a few things and hope one highlights the problem area. Firstly, deactivate all plugins and see if that solves the problem. If it does reactivate each plugin individually to determine which one might be causing the problem. You can then decide to keep the plugin, update it or find another plugin which is error free. This did not solve the problem, so onto the second solution…

I assume that you made a backup of your client’s WordPress files and database? You did, great! We can then rollback to the previous version of the website and see if the problem existed. This rollback showed me that things were working correctly before I had made my changes to their functions.php file. Here is the updated functions.php file with my code updates highlighting the problem.

I noticed that there were trailing whitespaces (blank lines) at the end of the file. I removed this as demonstrated in the next screenshot.

Trailing whitespace

I then tested the website again and to my joy was presented with the following screen. Everything was working! Brilliant I can now enjoy my day :)

trailing-whitespace-1

On bigger projects it is better to include code modules in a separate file, perhaps called ‘tweaks.php’ for example. You can put all your additional code here keeping your functions.php file nice and clean. Take a look at the Themeshaper WordPress Theme to see what I mean. This is best practice in bigger jobs, smaller jobs might not need it.

Got something to say?

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

Some html is okay