WordPress as CMS – Optimization

By Sol in Lab

It seems that in the process of getting the mass editing features working yesterday, I inadvertently borked something else! I thought everything was working fine, but when I reloaded the main page, I got no response, and php.exe was slamming my CPU. Not good. I wanted to get it worked out, but as tired as I was, I soon realized that I wasn’t getting anywhere, so I left it for the next day.

This morning, I attacked the problem again. Unfortunately, restarting my computer didn’t help ;). Deactivating the plugin, however, did help, so I knew where the problem lay. I was messing with a bunch of utility functions in my plugin yesterday, so I figured I must have forgotten a bracket somewhere and sprung a leak, causing the PHP interpreter to go into an infinite loop. I couldn’t find any issues with the code just by stepping through it so I removed all the code from the plugin, and re-added it back to the file one function at a time. Everything seemed to be working fine until I hit a function that fetches all the parent sections of a section. For some reason, when this function was called, the PHP interpreter was going crazy. Even still, I could find no problems with the code itself. Then it hit me, the database!

I checked the database, and sure enough, I found a section whose parent section was set to itself! I was manually changing values in the DB to test my mass editing code in different situations and I must have set the parent section of that section incorrectly (obviously). I’m not sure how it happened, but once I made the change, everything is once again working nicely (and seems a good bit quicker too, compared to when it wouldn’t load at all :)).

Once I got that bug out of the way, I moved onto optimizing the mass editing code. Like I said, the code was pretty messy as I was just focused on getting it working, so I ended up with a fair amount of repetitious code. This is abnormal for me, as I usually take the time to work out how best to structure things so that I don’t have any repetition. I guess I just wanted to get these features out of the way, so I didn’t plan for very long this time. In the future, I think I’ll need to remind myself to take the extra time it takes to get it right the first time, because going through messy code to find ways to optimize it was not fun. Ah, another day, another lesson 🙂

Thankfully, the optimized code is much cleaner and works quite well. Tomorrow, I think I’ll spend a little bit more time optimizing things a bit more and then move onto adding some new features (though I don’t quite yet know which features I’ll work on).