WordPress – Dynamic Copyright Date in Footer

Often you will come across sites with outdated copyright dates. Some sites show the current year as their copyright date. Both of these are annoying, and it shows that the site designer was lazy.

In order to give your users a little background info about your site, you should display the copyright date as such: © 2006 – 2010.

We can do this by simply pasting 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;
}

Once you add this function, then open your footer.php file and add the following code wherever you like to display the dynamic copyright date:

<?php echo comicpress_copyright(); ?>

This function looks for the date of your first post, and the date of your last post. It then echos the years wherever you call the function.

Leave a Reply

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

*