Anyone learning how to customize their Thesis website using PHP also needs to learn how to troubleshoot errors in their code. In this video I teach my 6 rules for preventing and finding errors in your custom_functions.php file. I also describe the work flow that I use when writing custom code for my own Thesis based WordPress websites. Those 6 rules are:
- Check your work as you go ( Rick’s PHP Workflow included)
- Use Netbeans to check for Errors
- Comment out Questionable Code
- Comment Out All Code
- Delete Unnecessary Code
- Start Over with a Fresh custom_functions.php File
Now what we’re going to do is we are going to move on to first, a conversation about troubleshooting. Now I actually made this little presentation for this morning ‘coz there’s a couple of people at the presentation this morning that I wanted to remind all this stuff of but they’re not here anymore and I didn’t have time to do that. So but I do want to take some time to, for a few minutes, to talk about troubleshooting your custom functions php file because this last week, I had a whole bunch of questions about php files that inexplicably stopped working. So it gave them the white screen of death you know, some of them walked out of their… the backsides of their sites. Some of them had annoying messages but you know, it still disturbed the display of their site. And it seemed to me like it was time to have another demonstration of how to troubleshoot.
So when you’re troubleshooting your custom functions php file, I have 6 rules. The first rule, it’s really the most important rule which is check your work as you go. You know, I’m not quite sure this is the case but it seems like there’s this long reluctance to use ftp and so people want to minimize the amount of time they spend uploading their file to ftp or uploading your file via ftp. However, when you are programming in php, you need to write your 1 function and then save it, upload it, and test it. And then if it works, then you move on to the next one. You always want to check the function before you proceed to the next one and you want to do this even when you know absolutely for certain that that function works, you still want to test it. Because if you always test after you create a new set of a php code then you’ll always know where your error is. So somebody who hates to spend time you know, 2 or 3 minutes that it takes to ftp his change to his site will spend hours trying to trouble shoot because he doesn’t know where the error exists. So he’s got 5 or 6 different functions that’s he’s placing together and… but he can’t tell where his problem is because he did all those in one fell swoop, uploaded it, his site crashed, and now he doesn’t know how to find the problem.
Well, if you check your work, check every function as you create it, then you always know that the last function you did was the one that broke your site. So that’s the 1st and most important step in troubleshooting your php code.
So I’m just going to reiterate something that you’ve probably seen many times now watching my videos, right. I always work on a local copy of my file, I never work on a live copy of my file. I write my php using an error checking text editor like NetBeans and then once I’ve completed that function, I save the local file, I upload to the site with Filezilla and I test the function. And somebody asked me here a couple of days ago yeah, I understand that that’s what you teach but do you have a shortcut for that? And the answer is no, I do not have a shortcut. I literally hundreds of times a day hit upload on Filezilla. I do it over and over and over and over and over again, there is no shortcut and there is no substitute for having immediate and live feedback on the work that you’ve just completed and on work that didn’t take you more than half an hour to do so you can… if it didn’t work out right, you know what you have to fix.
Okay, so rule number 2: Use NetBeans to check for errors. So NetBeans gives you 4 clues when checking for errors. The first clue is this error bar here on the right hand side. So we’re going to come over here to this file and you can see that it’s got this little red things here on the error bar and if you click on that, well, if you hover over it, it will tell you what the error is. And if you click on it, it will bring you directly to the line where it sees the error. So that’s your tool number 1, you see these little red things or if you see yellow ones… let’s see if I have yellow ones. Nope, I don’t. Then that’s your first clue.
Your 2nd clue is underline and so again, if you’ve made an error in code, you’ll get this little underline here under it and again, it will alert you the fact that you’re missing a required end tag for this. So the 2nd thing is your underline. The 3rd hint that you have a problem is icons at the line numbers. So you’ve got your line numbers over here and where you’ve got an error or a warning, you’ve got a little icon here. Now if it’s just a warning, then it won’t have this little red thing beside it. It’ll just have a light bulb and you can just hover over it, it’ll give you your hint. You can also alt enter, I believe that’s what it says. I did too slow but anyway, that’s what’s over here. And then your final one is the colors of the elements here.
His problem in particular was that he had a little navigation menu that wasn’t working and he had stuff saying that he had errors in particular, he was missing a closing tag and what he was missing was just a quotation mark like that. And once you are missing a quotation… well, I guess it wasn’t a quotation mark. It must have been something else. What was it he was missing? It probably one of these quotation marks, actually. Yeah, so now you can see that this used to be a blue text turned to brown, right? And then you come around here and this blue text and this green text is brown so that suggests that you have an error in this text that’s the wrong color and the error actually is dead here in the middle, it didn’t know that the title ended so it kept on interpreting this stuff as the title. But if you do that, you’re fine.
Now, when you do this…oh, let’s see, let’s put that back for a second here and let’s get rid of that one here. Now you can see you’ve got more red problems, right? Although now you see the little red squiggly down here tells you that it’s an unexpected symbol that’s found. It’s expecting to see the double quotation marks, not the greater than sign. But this is something that is very easy for a person to miss when they’re typing but is very easy for NetBeans to catch because NetBeans doesn’t always get it right. For example, this is not an error. It’s saying that there’s a missing closing tag but that missing closing tag is down here and there’s nothing intervening preventing that closing tag from being properly interpreted except that it’s got some perhaps, a little bit of non-standard stuff in here. Not even that, I mean, there’s really no apparent reason why it’s not recognizing these 2 things as belonging to each other so they’re indicating an error and it can indicate an error when there’s not really an error there. But you can satisfy yourself if that error doesn’t exist. Now, I did spend five minutes the first time I saw this, trying to solve my error but nevertheless, it does exist. It doesn’t always get it right but it gets the tough stuff to find right. Yes, it always catches the hard ones – missing tags and that kind of thing.
Okay, the 3rd step in troubleshooting is comment out questionable code. So the chances are, you know generally where the problem exists, right? It’s generally in this area here, someplace where you’ve got weird squigglies or missing colors. Even if you can’t find the problem, you can just take the entire function from top to bottom all the way down to the add action to that function and you can use this tool right here to comment it out. When you select that, you will have commented…yes, you will have commented out that entire function and nothing will… this will no longer be processed. And then, you can try it again just to verify that this is actually the problem child and if the problem still exists after you’ve commented out the function, then you know it’s not the problem child. Okay so comment out questionable code and comment out the entire function including the add action statements and then test it again.
So then the 4th step in that, if that doesn’t get into it, is that you can comment out all of the code. So then you could take your entire file, just start on one side. In fact, you can just take this little tag right here in full copy, stretch it all the way down to here, control V and now the entire…oh, I didn’t comment on the entire thing because it’s intervened with these other comments. But you get the point. You can comment out everything on your site and then gradually add the functions back in. Now, if you are going to end up doing this, you want to start by uncommenting those functions that you believe are safe. So you know, some of them that you’ve worked with in the past that looked the same as they were or that were there and were never edited, you can start uncommenting those first and then test it. And then it’s just disabling plugins and then reenabling plugins – you would disable functions and reenable functions and then you just keep on doing that until you find the offending code.
Well, I had a problem this morning that this solved and that is delete unnecessary code. So this person had a problem with their code, they were getting an error message on a line that didn’t have any code in it and they had comments and some… and the default code from the original custom functions php file in it and that they weren’t using. And I just said, “Okay, just take everything below the line that you are using and delete everything below that. So take out everything that you aren’t using.” And once he did that, his site worked fine. So that’s deleting the code, deleting the comments and it’s especially important ‘coz this often hangs people up, is to select and delete everything after the last function. So if you’ve stopped at you know, a single function, make sure there’s nothing below that when you do that.
And then the 6th and final rule, and this is how I had to solve a problem this weekend, was start all over again from scratch with a fresh custom functions php file. Now, I do not know why Pam’s php file didn’t work. She showed… she pasted her code in the forum; the code looked fine so I had her email me her file. I opened up her file in NetBeans, it didn’t show any errors. I used her file on a demonstration site and it broke the site, on my site just as well as hers. And so what I did was I just took a copy of a backup out of the custom sample folder and I pasted the functions gradually back in and tested it and it worked. So there was no obvious reason why there was a problem with this file. She wasn’t doing anything wrong. There was some hidden problem with the file that NetBeans didn’t see, I couldn’t see, and that could only be resolved essentially by starting over from scratch and cutting and pasting code into that file. Now the trick is, don’t just copy the whole file because you could be copying the error. What you do is you just grab the function and its corresponding action statement so for example, you would grab this… pardon me… you would grab this but not that, right? So you’d leave… you wouldn’t grab any of the extra lines. You would just grab that you absolutely needed and one at a time, paste those functions into the page, test it, see if it’s working, and then just keep on going. And that was the 6th thing that we did and that was what worked.
So that is my little presentation on checking or I’m sorry… on troubleshooting your custom functions php file and hopefully, you will have found that to be useful.