Build Your Own Business Website header image

Lesson 9 – Part 2 – Anatomy of the Thesis custom_functions.php file

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects -

Applies to -

The next thing we’re going to talk about is the Thesis theme custom_functions.php file. This is using Thesis 1.8.4 and this is the NetBeans IDE version 7.0.1 and I’ve opened up my custom functions file in that.

Elements of the Custom Functions File

Your custom functions file has a couple of different elements. It starts off with this opening php tag. Now this opening php tag is absolutely crucial for understanding how this page works. I mean, it’s something that you can set and forget but it is the first line of instruction for your web server. Without this, your web server doesn’t know that you’re doing php code because a php file is capable of containing HTML and javascript inside of the php file.

Opening PHP Tag

So the php needs to recognize or your web server needs to recognize where the php functions start. And so that’s why every php file begins with this opening php tag. The reason why I talk about this is because the php tag has to be the very first thing. There can be no spaces. There can’t be another line. It’s got to be the very first thing in the file, at the very top corner of the file and there can be nothing else on the same line.

So if there was a space like this, this file would fail because there can be nothing. No white space, no spaces, no characters, no text. Nothing can follow this opening php tag until you get to the second line.

Closing PHP Tag

Now it is tradition, outside of WordPress that a php file has a closing php tag as well. And the closing php tag is sort of the opposite of this. It’s the ?>. That’s your closing php tag and this is the traditional way in which a php file is begun and ended.

However, in WordPress, we generally do not use a closing php tag for a variety of reasons. One of them is if there is anything after this closing tag, this file will fail. So if you’ve got an opening php tag and a closing php tag and a space after that, it doesn’t matter what it is, if it’s a dead space, a line break after that, this will throw an error.

However, if you don’t have a closing php tag, it doesn’t matter how many lines you have. This will not throw an error. And so in WordPress, we generally do not have a closing php tag even though if you were to go and take a class at, you’d learn it starts with an opening and ends with a closing. In this case, we only have an opening php tag.

Comment Block

Next, you have a comment block. This little comment block has gotten smaller over time. Back in Thesis 1.6, there was a lot of stuff here in the comment block. And what you have here is a couple of different choices in PHP for adding comments.

Comments are very critical and very important in writing your code. So it’s important that you understand how to use comments. A single comment either starts with a double slash and this is a single line comment… or a comment block which is what you see up above here, starts with a slash and an asterisk and end with an asterisk and slash.

In NetBeans, if you start with a slash and an asterisk and then add a comment block and hit return, it will add a new line and add a closing tag. You don’t really have to remember this. You can just start the opening tag and then add some more code or comment and then hit enter and then add another comment. So this works nicely.

Comment Out Code

It can also be the case that you may have a piece of code that you want comment out. Let’s just open up an old custom functions php and I’ll show you an example. Let’s say I’ve got this little block of code and I want this comment this block of code out. In NetBeans, you can simply select it, come over here to the comment button, select comment and now it’ll make each one of those a single line comment.

And if for some reason or another, you want to un-comment, you can put your cursor in one of the lines and it’ll un-comment it. Or you can select all of them and it’ll un-comment it. Of course, it can’t select something that’s not commented and un-comment it.

So that’s how you can use a comment block or use the commenting and uncommenting function in NetBeans with a block of code that’s already been written. This is a very useful function when you are troubleshooting. You may want to turn off some piece of code while you’re trying to figure out what the problem might be.

Where to Add Code

Then finally, where do you add your code? Well, Chris has placed this nice little thing here. ‘Place your custom code below this line.’ If you’re going to place code, you’re going to place code separate from all the rest of your code.

Now I suggest a system where you imagine your code as acting on a page and you put that stuff that happens at the top of the page first and the stuff that happens at the bottom of the page last. That’s a fairly simple mechanism for how to organize your code.

Organizing System for Your Code

You should have some kind of organizational scheme so that you can figure out where your functions are in the future. So if you edited something 6 months ago and you want to come back and find where that function is, if you have a system for laying out your custom functions php file then it’ll be relatively easy for you to find it. If you just randomly put your functions wherever you put them then it can be a little bit more difficult to find, especially the complicated ones.

I recommend that something that happens before the header is at the top and something that happens after the footer goes at the bottom and everything in between sort of follows the layout of a page because that’s pretty much how you’ll be writing this code anyway.

In the class that I’m teaching next week where we start how to create a child theme in the Thesis theme, we’re using multiple files. I’ll be teaching a different system or organization in that case because we’re not just using a single functions php file. We’ll be using a structured file system that uses many files. But for simple edits on a simple site, this is what I would do.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one