In example #4 of ways to use Action Hooks to customize the Genesis theme we refine the changes we made to the footer in example #3. In that example, we removed the existing attribution and added our own along with a copyright date.
Use WP Function bloginfo to Automate the Name
What we’re going to do now is automate the name of the website and then the current date. First, we’re going to automate the name of the website, Customize Genesis 2.1. What we’re going to use is the WordPress function called bloginfo.
Ways to Use bloginfo
This is what it looks like, it’s just bloginfo and then parameter that you want to show. What bloginfo does is just prints to the screen some piece of information about the website. Let’s look at all the parameters for using it at codex.wordpress.org.
So bloginfo ‘name’ is going to show the site title, bloginfo ‘description’ which will show the tagline, bloginfo ‘wpurl’ will show the WordPress URL or URL shows the site URL. Generally it’s the same thing. And so on. There are all kinds of little things in here that you might extract and display from bloginfo.
Add the Code to Automate the Site Title
What we’re going to do is use ‘name’. So I’m actually just going to copy this. It’s set up exactly right for us to use here. We’ll add it right in the middle of this paragraph tag. We’ve got our opening PHP tag then our WordPress function and then the value that we’re going to place in that which in this case, is ‘name’.
It needs to go inside of the parenthesis and inside of quotation marks and then it has the closing semicolon for that statement and then a closing PHP tag. You could make it a little bit more complicated if you like by doing something like this, Copyright and © then <?php bloginfo ‘name’ and 2009-2014 Designed by…like that. It’s all essentially the same thing.
These returns here don’t mean anything, it’s just a way of breaking down the code a little bit which makes it easier to read. In this case, we are opening PHP here because remember we closed it here. So we’re opening it up again putting in a function, closing it and then going back to HTML.
So let’s upload that to the site and take a look at that example. Now you can see here in the footer, LEARN HOW TO DEVELOP A SMALL BUSINESS WEBSITE USING GENESIS 2 which is the name of the website. It took that site title and printed it out here.
Use Date Function in PHP to Automate Copyright Date
The other thing I want to do is automatically increment this and to do that we are going to use the date function in PHP. This time we’ll go to php.net. Here it is, date. This is a little difficult to read actually. You have to understand what they’re trying to demonstrate here but essentially it is date and then the format that you want the date to display in.
Format is set up by character. So a capital “M” is January in a 3-letter. A small “m” is the month in 2-digits. “F” is the full name of the month. We are going to use capital “Y” which is the full numeric representation of the year in 4 digits, 2003.
Add the Code to Automate the Date
It’s actually a lot simpler than this makes it look because in fact, what we’re doing is again essentially what we did with bloginfo. But it’s date and instead of ‘name’ it’s capital ‘Y’ so date ‘Y’. Actually, having a look at this I think what I really want is that down here.
This is going to give us the current year. So we’ll save that and refresh this. Whoops, it didn’t work, 2009 – <?php date. That’s funny. Let’s see what we’re missing here. Date manual, it’s echo date.
That’s why I had that echo there to demonstrate because echo, as you may recall from our first Action Hook example “Hello World”, means print the screen and it is actually, echo date. Bloginfo automatically writes to the screen.
Echo Statements: WP Functions vs Typical PHP functions
Lots of WordPress functions do that, they will automatically write the thing that you want to the screen but typical PHP functions do not do that. They require some kind of an echo statement or return statement in order to do that. So it’s echo date ‘Y’.
Let’s upload this and take a look at the finished product. Now you see 2009 -2014 DESIGNED BY BYOBWEBSITE.COM and this will automatically update the date and if you change the name of your site, the name here changes.