Dynamic Copyright

Learn Putting a Dynamic Copyright Date in the Footer of WordPress

Advertisements

Frequently, you will come across a website with an out-of-current copyright date, which is rather aggravating. There are however websites that merely display the current year as their copyright date, which is even more inconvenient because you have no idea how old the website is. Most developers are familiar with a simple PHP solution for this, but there is a more elegant alternative that we will show you. We’ll show you how to use a function to automatically produce a copyright date based on the published dates of your oldest and newest posts in this article.

Dynamic Copyright
Using PHP to Create a Dynamic Copyright Date

You’ll need to add some code to your WordPress theme files for this method to work. Check out our instruction on how to copy and paste code in WordPress if you haven’t done it before.

The most frequent technique to display a dynamic copyright date is to change the footer.php file in your WordPress theme. Simply copy and paste the code below into the line where you want the copyright notice to appear.

1
<p>&copy; 2020 – <?php echo date('Y'); ?> YourSite.com</p>

The issue with this code is that it cannot dynamically retrieve your site’s start date, therefore you must have a site that is at least a year old to utilize it.

For a dynamic copyright date, an elegant WordPress solution is available.
We came across a more elegant option proposed by @frumph of CompicPress Theme while exploring the web.

Advertisements

Based on the published dates of your oldest and newest posts, this code will generate a dynamic copyright date. If your site is in its first year, this feature will simply show the current year.

To do so, first add the following code to your theme’s functions.php file or a site-specific plugin’s functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "&copy; " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

After that, add the following code to the footer.php file of your theme where you want the date to appear:

1
<?php echo comicpress_copyright(); ?>

The following text will be added by this function:

© 2009 – 2021

Make sure your copyright dates are up to current. Utilize this strategy in your present and future WordPress sites.

Advertisements

Leave a Reply