Now we’re going to move on to example number 2. In this example we are going to change the Thesis theme footer. Let’s look at the Thesis footer here. Here’s our site that we’re working on and this down here is the Thesis footer.
HTML Structure of the Thesis Theme Footer
And if we inspect the element, you get an idea of the HTML structure of the footer. Div id footer is the HTML structure and inside of that div id footer is this p tag that has the “Get Smart with DIY Themes” and this is what is known as the Thesis attribution. And then also inside of that is a link to the log in screen.
Remove Thesis Attribution
So those are the 2 elements. Each of them is essentially a set of paragraph tags and this paragraph tag is fired by a function called Thesis attribution. As you may well guess, what we’re going to do is remove the Thesis attribution with a remove action. This is a Thesis theme option, this WordPress admin and we’re going to use Thesis options to get rid of that first because there isn’t a simple way of getting rid of it otherwise.
If we go to Thesis Design Options and then go down to administration, show admin link and footer, we’ll uncheck that. Hit save, go back to our page and now that’s gone. And now what we’re left with is this Thesis attribution that we’re going to remove. So the first thing we’re going to do is remove it and then the second thing we’re going to do is replace it with something else.
So again, we’re going to use remove action and this is Thesis hook footer which I meant to point out actually. Come all the way down here, Thesis hook footer. This is the hook, this is hook 2. So remove action, Thesis hook footer and then the action that we’re going to remove is Thesis attribution. We can save this, upload it, come back over and refresh the page and there it is. The attribution is gone. So now we’ve removed that.
Write the First Function
A function starts off with a keyword function. We start off with that and then it has a function name and the function then is wrapped inside of brackets. So I’m going to do this first, byob simple footer attribution.
Anatomy of the Function
So there’s the bare bones of our function. It’s the keyword function that tells PHP that this is the definition of the function. Here’s the name of the function and a set of parenthesis which are necessary but but we’re not going to talk about that today. But parameters can be placed inside the parenthesis.
That’s exactly what’s happening here, remove action is really just a regular WordPress function, remove action with these parameters. And so this is essentially a function that doesn’t have any or doesn’t require any parameters at this point which is what the majority of your functions are going to be, functions that don’t require parameters.
The last part of making a function work is the part that calls the function or the trigger that calls the function. It’s the add action and it’s going o be Thesis hook footer and then our function name which is byob simple footer attribution. Actually, I should probably use the same terminology as I used in my sample files. So it’s actually byob custom footer, simple custom footer.
So we have our add action. It’s adding this function. It’s adding this function to this hook. Now we got to put something in the function. Right now, it’s empty.
Naming Conventions for Function Names
There is a set of naming conventions for function names because function names all have to be unique. If you have 2 functions in WordPress that have the same name, WordPress will crash, well actually, PHP crashes. The same thing is true inside of PHP, that function names must be unique.
Unique and Descriptive
And in addition to that, function names should be descriptive. So, my custom footer might be descriptive but it’s far too generic to be unique because the world is full of people with tutorials on how to do footers that start off with my custom footer. So you don’t want to use function names that could clash with something else that you might do in the future or with something else that somebody you hire might do.
You want to make sure that you use unique function names. So in this case, I always preface my function names with byob, the first 4 letters of my website, BYOB Website, Build Your Own Business Website.
It doesn’t really matter what you do. You could use your initials. You could use the name of the site. You can do anything but it is required by WordPress themes in the WordPress repository and by WordPress plugins that all of their function names are prefaced with a unique identifier.
And so in my case, it’s byob and then a descriptive explanation of what this is. This is my simple custom footer so that’s what I’m saying here. By the time you end up with 15 or 20 functions, you’ll see that you want to use descriptive function names. And chances are, somewhere down the road, you’re going to hire somebody maybe to help you with your site. And if you’re not using descriptive function names, they aren’t going to be able to figure out what your function does. This helps make your code self-commenting.
All Lower Case and No Spaces
So anyway, simple and unique. The function names also should be in all lower case. When you see upper case letters in a function name, that’s actually a class name. In WordPress, the convention is all function names are lowercase. You don’t use camel case. Camel case is something you may have seen. This would be camel case, byobSimpleCustomFooter.
There are applications in which you use camel case but a WordPress PHP function is not one of them. So it should be all lowercase, byobsimplecustomfooter, and there could be no spaces.
There should be no special characters. There can be numbers. So the name should be relegated to lowercase letters, underscores, and numbers. You should not use hyphens in a function name. Hyphens are used for CSS classes and for file names but should not be used inside of function names. So it should be byob_simple_custom_footer. Okay?
And then you have your opening and closing bracket. If you don’t have these opening and closing brackets, PHP doesn’t know where the function ends. PHP knows your statement ends with a semi-colon. Your function ends with a closing bracket.
Add HTML in the Code
Now we’re going to insert a little bit of HTML inside of this php code. Jared asks why do I have all these empty spaces in here. Well, what you do when you write PHP is you build a skeleton and then flesh it out. And so you start with the opening and closing and then give yourself some room and then you progressively refine it. These spaces just give me space in order for me to add my refinements.
In order for me to use HTML in this, I need to escape the PHP. And you escape PHP by closing PHP and then by opening it back up again. So to close PHP, you use the closing PHP tag. That’s that. Now notice it throws errors down here because this is PHP code but I’ve just escaped it. In order for this code to actually function properly, I have to enter PHP again.
So there’s my opening PHP tag. Now whatever happens inside of this is not PHP. So we’ve got PHP all the way down to here and then not here and then PHP starting up again. So we’ve escaped it and then we’ve restarted it.
Good programming practice requires you to indent so that you can see what’s going on where. Our first function is not indented but then everything inside of the function gets indented. And so we’ve indented our PHP so we’re going to tab over to retain that indentation.
And now what we’re going to do is write HTML code. And the code we’re going to write is opening and closing p tag and let’s go ahead and make sure it indents it. Okay and then I’m going to say Tailoring the Web and then I want to do the copyright symbol.
The copyright symbol is a HTML special character. If you Google HTML special characters, you’ll see that there are lots and lots of HTML special characters. But the HTML special character that we want to use is ampersand copy semi-colon. That will produce that circle with a C around it. So there’s your HTML special character and then 2009 to 2012. All rights reserved.
What we have now is an HTML block of text that’s inserted inside of this PHP function that is going to write this and this function is going to be hooked to the Thesis hook footer. And so we’re going to see this down there at the bottom.
Let’s come over here and save it, back up at the top and go over here and refresh it. There it is. Tailoring the Web copyright 2009 – 2012, All rights reserved.
That’s the simple custom footer for the Thesis theme.