wpthemedetector-logo
WordPress Theme Detector Blog
add-functions-child-theme-2

Add functions to your WordPress child theme (2) – Create a new function

10 comments

Ok here we are, back to our “Add functions to your WordPress child theme” tutorial series.

In the first post from this series we learned how to modify the functionality of an existing function, but we did not create any new one. We will be taking care of that on this second tutorial.

We are not going to deal with filter and action hooks yet, in order to keep new concepts as clear as possible for every step we take towards mastering child theme functionality.

The example I am proposing today will need a function to make some things, and then we will just display its result by echoing it.

What do we need our new function for?

There are tons of possible examples about functions that could be added to our child theme. Functions tend to be thought of as something belonging to the realm of plugins, but we won´t want to have dozens of plugins installed if each one of them is just used to perform a small task that could also be handled by a theme function.

You may have seen a copyright notice at the bottom of many websites. Maybe you are already displaying a line like that on your own blog. Do you update the current year manually? The function I will use to illustrate this tutorial will do that for you automatically.

“My Updated Copyright” function

Our new function will output a copyright notice that can be displayed anywhere we want on our website, showing the copyright symbol (©), the year our site was launched, the current year and our name. In our particular case we will echo that notice on the footer of our site.

The notice that this function will generate shall be dynamically updated. As we will see, such a function can be coded using at least two different approaches, with or without querying our site database. Using the first approach our function will get the current year automatically, but we have to tell the function what was the starting year. The second approach needs to query our database, but the function will get both dates by itself.

Once again, for this tutorial we will use the same TwentyTwelve  child theme I´ve used before on Add functions to your WordPress child theme (1). Up to now, the footer on that theme only shows the default “Proudly powered by WordPress” link from TwentyTwelve:

add-functions-child-theme-original-footer

The copyright notice that we will get from our new function will be added before that default link, at the bottom left corner of our site. That position, however, will not be due to the function itself since our function will only generate the copyright notice, which could then be displayed anywhere on our site.

Adding our new function

It is time now to open the functions.php of your child theme on your text editor and add our new function, which I´ve named my_updated_copyright. We need to pass it at least one argument: the claimer of the copyright, which for the sake of simplicity we will consider a character string (as the site owner´s name). So if we use the variable $owner for that parameter, our function will be my_updated_copyright($owner) .

As we have not created any brand new function yet but only copied and modified an existing one so far, for the less techie readers I will say first that the PHP code needed to add any function should begin with the function declaration, that is the word function followed by the function name with its input arguments between brackets (comma separated if there are several arguments), and then an opening brace (curly bracket, {). All the function code should be placed between that opening brace and a final closing one, like this:

Coding our new function – Approach #1

The first (and simpler) approach will need us to tell the function what was the year when we launched our site, while the function will return the current year. So our code will have to do the following things:

  1. Receive from us the name of the copyright claimer (person, organization, domain…) to be displayed.
  2. Receive from us the starting year.
  3. Get the current year.
  4. Determine whether the current year is different from the starting one.
  5. Construct the text line that will constitute our copyright notice by including the copyright symbol (©), the year range (for example 2010 – 2013, or just 2013 if our site is new) and the claimer (for example your name).

Since for this first approach we need to pass the function not one but two parameters, it will become  my_updated_copyright($first_year, $owner). Here is that code:

And here is how this function works:

  • Line #3 defines the variable $copyright_notice which will finally be returned by our function, beginning the construction of the text line by appending the starting year of our website (passed to the function by means of the $first_year argument) after the copyright symbol © (©), leaving an empty space in between.
  • Line #4 defines the current year variable by using the PHP function date(), which returns the current date as a formatted  string. In our case we are only interested in the year of the current date, so the format argument we are using is 'Y', which returns a 4 digit full numeric representation of the year.
  • Line #5 (with its closing brace on line #7) is a conditional structure: the code in between those braces will only be executed if the expression between the if() brackets is true. In this line we are checking if the first and the last years are different.
  • Only in that case will line #6 be executed, appending at the end of our copyright notice a dash between empty spaces and finally appending the current year.
  • Line #8 finally appends one more empty space plus the content of the variable $owner which was passed to the function, thus completing our notice.
  • The last thing our code has to do is to tell what will be the result returned by our function (of course the variable which contains our copyright notice), and that is the purpose of line #9.

Making our function work

Since we are going to display our notice at the bottom of our site, we need to modify our footer.php file. But of course we are working with a child theme and we don´t want to change anyone of the files of its parent theme, so what we will do is copy the parent´s  footer.php and place that copy (with the same name) in our child theme´s root folder, at he same level than the other files that we have created so far (style.css, functions.php and the screenshot, if you have been following my previous child theme tutorials).

In our  footer.php file copied from the TwentyTwelve theme we have to identify the lines of code that display the WordPress link, because we want to show our copyright notice right before that link.

The lines we are looking for are line # 3 and line#4 on the above code, both wrapped by a division with the class “site-info”. All we have to do is to echo the result returned by our new function right before those lines, telling our function what we want to display as values for the function arguments $first_year and $owner, like this:

Just by adding that line we will make our function work and we will get the desired result:

add-functions-child-theme-modified-footer

We have passed that function a name to be displayed after the year range, but we could also have used a link instead of a name if we wanted to (for example the link to the author´s personal site), or we could even link the whole copyright notice.

Let´s give it some style

If we wanted to display our copyright notice with a different style, all we have to do is wrap it with a CSS class. In my case I thought of giving it some air by separating it farther from the WordPress link, and giving the text some particular color, for example some kind of blue (hey, that´s a must-have Miles Davis record by the way ;)).

So I just enclosed the line I had added to footer.php between span tags, with a new class that I´ve named “site-copyright”:

This is what I added to my style.css file to define that class:

And here is the final result:

add-functions-child-theme-styled-footer

Coding our new function - Approach #2

The second approach to our function is based on the dates of our earliest and latest posts. The function will get the years from those dates, which are obtained by means of a database query. Therefore, the code we need for that function has to do the following things:

  1. Receive from us only the name of the copyright claimer.
  2. Get from our database the publication year of our oldest post or page and that of our newest one.
  3. Determine whether the current year is different from the starting one.
  4. Construct the text line accordingly.

The code we need in this case is:

The final lines are similar to those of the code used by the first approach, the main differences being at the beginning of the function code: Lines #3 to #9 are needed for the database query (in fact they are only 3 lines, but #4 is split in separate lines for the sake of clarity). Then lines #10 and #11 extract the desired value for the years from the object resulted from the database query.

Since now we only have one argument for our function, the line to be added to footer.php will be in this case:

Which function works better?

It is up to you to decide which function you prefer to generate your copyright notice, the visual result will be the same in either case.

The second one is more automated, and that is why I included it in this tutorial. But I personally like the first one better, for the following reasons:

  • It does not need to query the database, which is always better in terms of performance.
  • Imagine we were some time in January 2014 and we had not published any new post since December 2013 yet. If we used the first function code, our copyright notice would correctly show 2014 as the final year, while the second approach code would force our notice to still show 2013, because that is the year of the newest post.

Conclusion

In this tutorial we have learned how to add a function to our child theme, this time creating a brand new one from scratch. In fact, we have seen two different versions of code for that function (don´t  give them the same name if you want to try both on your site!).

We did not need action or filter hooks to make our function work because the function we created just returns a text line that we could echo where we need it to be displayed. We will learn what those hooks are and how to use them in future tutorials, so stay tuned!

Luis Alejandre

Luis is the creator of the WPThemeDetector tool and the chief editor of this blog. You can read more about Luis in our About page.
Liked this post? Please share it!

10 Comments

  1. Thanks Luis,

    I have just implemented this code on our site.

    Waiting to see now if the stupid Google spider prepends the 2005 start date to the beginning of our meta description – again.

    Was previously using code from this post: http://www.wpbeginner.com/wp-tutorials/how-to-add-a-dynamic-copyright-date-in-wordpress-footer/

    If Syed approves my comment; I had a fair old whinge there about how Google thinks their spiders are smart enough: to pick a correct date from the page – and display it at the beginning of your meta description.

    This mistake by smart-ass google spiders, has so far cost us 1 month in lost sales.

    This is my last attempt to correctly display our copyright date range.

    If Google stuff this up again – I’ll have no option but to permanently remove the start date of our blog.

  2. very helpful! Shouldn’t the “span” tag above (under “Let’s give it some style”) include the class name?

    • You are absolutely right, Steve. I´ve already corrected it on the post.
      Of course, the actual footer.php included the class name (otherwise that copyright line wouldn´t have got its styling!)
      Thanks for pointing it out.

  3. Wow!Its pure joy for me to be able to do that.I am trying to understand WordPress functions and you explain it so clearly that it seems easy.So,Even though it is a small step,I love what I learnt through your tutorial.Thanks!

  4. Hi,
    I would be interested to learn how to delete an existing function. With no child theme, I used to comment the line with the function I want to disable. With a child theme how can I disable a function that appears into the parent theme ?

    • Hi Benoit,

      You just have to remove that function by including this line to your child theme´s functions.php:

      < ?php remove_action( $hook_that_triggers_the_function, $name_of_the_function_to_remove); ?>

      Here is the link to read about remove_action in the WordPress Codex.

      I hope this helped you.

  5. Thank you Luis. What about removing a “require” ? For example how can I remove a declaration like this from the functions.php ?

    require get_template_directory() . ‘/inc/styling.php’;

    • I think you may or may not be able to “unrequire” that file depending on how that require is called. For instance, is it inside a hooked function?

      But even if you were not able to remove it, maybe you could be able to override the functions contained in that php file.

  6. Luis, your method about removing action in a child theme just doesn’t work. The principal reason is that you try to remove something that hasn’t been declared yet (because your child functions are loaded before the parent one). Your reference to the WordPress Codex is written for Plugins not Child Themes. Here is maybe the right way to do it (I tried and it works!): http://www.marcusnyberg.com/2012/02/03/using-remove_action-in-a-wordpress-child-theme/comment-page-1/

    • You´re right Benoît, thank you for pointing it out. I´m sorry I confused you, I actually didn´t have to use that function for that purpose yet so I hadn´t tried it myself for that purpose (it just seemed to me the obvious thing to do, but I was wrong).

      That tip by marcus Nyberg seems perfectly right (it is actually right, since it worked for you). The hook after_setup_theme is perfect for this kind of purpose.

Leave a Reply