(+84) 463.28.7979

How to Add a Dynamic Copyright Date in WordPress Footer


Often you will see a website that has an outdated copyright date which is pretty annoying. There are also sites that only show the current year for their copyright date which is even more annoying because you won’t know how old the site is. There is a simple PHP solution to this that most developers would know, but there is a more elegant way that we will show you. In this article, we will share a function that will automatically generate a copyright date based on the published date of your oldest and newest post.

Simple PHP Solution for Dynamic Copyright Date

You would paste something like this in your theme’s functions.php file

&copy; 2009 – <?php echo date(‘Y’); ?> YourSite.com

The problem with this issue is that you would have to add this once your site is at least one year old.

Elegant WordPress Solution for Dynamic Copyright Date

While surfing the web, we saw a more elegant solution suggested by @frumph of CompicPress Theme. They are using this function on their excellent ComicPress theme. This function will generate a dynamic copyright date based on the published date of your oldest post and your newest post. If it is the first year of your site, then this function will only display the current year.

To implement this dynamic copyright date in your WordPress footer, open your theme’s functions.php file and add the following code:

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;
}

Then open your theme’s footer.php file and add the following code where you want to display the date:

<?php echo comicpress_copyright(); ?>

This function will add the following text:

© 2009 – 2010

Don’t keep your copyright dates outdated. Take advantage of this technique in your current and future WordPress sites.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>