From the course: WordPress Development: Coding Practice

Dynamic copyright year

- [Instructor] We are going to start off with a common problem on websites, a hard-coded static copyright year that needs to be updated manually. Many websites require manual updating of the copyright line when the calendar year changes. So the requirements for this challenge are to create a function using the provided code that dynamically gets the current year and displays it. You should pick a start year for the copyright line, I'll be using 2003 because that's when I first started my blog. As a quick note, many of these examples will include short codes for displaying the results. That way we will be fully backwards compatible with WordPress. We don't need to worry about JavaScript or block development, and we won't need to worry about sites that do or do not support full site editing. Your starting code can be found in 01-01-start. All of the challenges will be plugins that you can add to WordPress. Use this as your starting point. Pause the video here and unpause it when you're ready to see my solution. (upbeat music) Welcome back. So this is going to be a one line solution and because we're using a short code we want to return the value. The short code functionality in WordPress will make sure that the information is outputted. So we're going to type return and then we'll use single quotes. I will use the HTML entity for the copyright symbol which is ampersand, copy, and then the semicolon. I'm going to write 2003 space dash and then space. Remember, we want to include a space inside of our static string inside of our quotes so that we don't have text colliding with one another. And then we'll add the period for concatenation. We'll use the built in date function in PHP with a capital Y to display the four year or the four digit year. Then a semicolon. We'll save this and then we will go ahead and upload it to WordPress and give it a test. So I have already installed the plugin over at plugins, add new, I've uploaded it already and I have a page that I'm using as a test page in my WordPress installation where I have the short code in the short code block. We'll click update and then we'll view this page and you'll see the copyright line displayed here. If you are using full site editing, you could always add it to the template, but again, we don't want our code to rely on that.

Contents