Add Snippet To Project
Do you want to ensure that the copyright year in the footer of your WordPress website is always up-to-date? Manually updating the copyright year every year can be a tedious task, especially if you have multiple pages or a large number of posts. In this article, we’ll show you how to add a dynamic copyright year to your footer, so it automatically updates each year without any manual intervention. With this simple solution, you can save time and effort while maintaining a professional and current website.
function wpturbo_dynamic_copyright_year() {
$start_year = 2021;
$current_year = date('Y');
if ($start_year == $current_year) {
echo $start_year;
} else {
echo $start_year . '-' . $current_year;
}
}
add_action('wp_footer', 'wpturbo_dynamic_copyright_year');
The code snippet provided is used to display a dynamic copyright year in the footer of a WordPress website. This is a common requirement as the website owner wants to automatically update the year without having to manually change it every year.
The wpturbo_dynamic_copyright_year()
function is defined with two variables: $start_year
and $current_year
. The $start_year
variable holds the initial year that you want to display, in this case, it is set to 2021. The $current_year
variable uses the date()
function to fetch the current year from the server.
Next, there is an if
statement to check if the $start_year
is the same as the $current_year
. If they are equal, it means it is only the current year, and we only need to display that single year. In this case, the echo
statement is used to output the $start_year
.
If the $start_year
and $current_year
are different, it means the website has been around for multiple years. In this case, an else
block is executed. The echo
statement is used to output a string combining the $start_year
, a dash (-), and the $current_year
. This will display the range of years, such as "2021-2022", indicating that the website has been active for multiple years.
Finally, the add_action()
function is used to hook the wpturbo_dynamic_copyright_year()
function into the wp_footer
action. This ensures that the dynamic copyright year is rendered in the website’s footer when the footer is being generated.