Family Encyclopedia >> Electronics

How to add dynamic copyright date in WordPress Footer

Often you will see a website that has an outdated copyright date which is quite 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's a simple PHP solution to this that most developers would know, but there's a more elegant way of displaying it. In this article, we will share a function that will automatically generate a copyright date based on the published date of your oldest and most recent publication.

Simple PHP solution for a dynamic copyright date

You would paste something like this into your themes. functions.php file

 © 2009 - YourSite.com 

The problem with this problem is that you will have to add it once your site is at least a year old.

Elegant WordPress solution for a dynamic copyright date

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

To implement this dynamic copyright date in your WordPress footer, open the functions.php theme 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 =" © ". $ 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 footer.php file and add the following code where you want to display the date:

 

This function will add the following text:

© 2009 - 2016

Don't keep your copyright dates out of date. Take advantage of this technique on your current and future WordPress sites.