This is the time of year when bloggers go scrambling through their code, updating the copyrights on all their pages to something like this:
Copyright © 2009 Example Blog, Inc.
And unless they completely rewrote their website on January 1, this is wrong. Some use a more all inclusive notice:
Copyright © 2001-2009 Example Blog, Inc.
As common as this is, it’s not particularly accurate. The range may be true for the entire blog, but on an individual post’s page (like that first one you wrote in 2001 and never touched again) it’s likely not.
It would be better if the copyright at the bottom of the home page was for the entire site, and on other pages it reflected what was on that page. Better still, we’d like it to all happen automatically at the stroke of midnight on January 1. That’s what I’m going to show you how to do, using WordPress.
I’m doing this with the default theme in WordPress 2.7, but you shouldn’t have any trouble adapting it to your own theme. We’ll start in header.php and add two lines of PHP code:
< ?php
$copyright = '';
define(_FIRST_YEAR_, '2001');
?>
This block can go anywhere in header.php. All we’re doing here is creating a variable, $copyright, that we’ll populate later and defining a static variable, _FIRST_YEAR_ that will represent the year we started the blog. We could calculate the first year, but this is simpler.
Then, we’ll output our copyright in footer.php. I threw it in below the WP links:
<p>
< ?php bloginfo('name'); ?> is proudly powered by
<a href="http://wordpress.org/">WordPress</a>
<br /><a href="<?php bloginfo('rss2_url'); ?>">Entries (RSS)</a>
and <a href="<?php bloginfo('comments_rss2_url'); ?>">Comments (RSS)</a>.
<!-- <?php echo get_num_queries(); ?> queries. < ?php timer_stop(1); ?> seconds. -->
<!-- Copyright -->
< ?php
global $copyright;
if ($copyright) {
?>
<br />Copyright © < ?php echo $copyright; ?> Example Blog, Inc.
< ?php } ?>
</p>
We check if $copyright has a value and if it does, we output our copyright notice. The next thing we need to do is set that variable. We’ll start in single.php which is the simplest. All we need is a couple lines of code somewhere within The Loop:
< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>
< ?php
global $copyright;
$copyright = get_the_time('Y'); ?>
The function get_the_time() works just like the_time(), but returns a value we can work with, rather than outputting directly to our page. This same code can be used in page.php.
For archives, we want our copyright notice to reflect the posts on that archive page. Towards the top of archive.php, we’ll define an array variable to store the year of each post on the page:
< ?php $years = array(); ?>
And we’ll populate that array within The Loop:
< ?php $years[] = get_the_time('Y'); ?>
Finally, after the end of The Loop, we’ll send that array to a function that will populate our $copyright:
< ?php endwhile; ?> < ?php global $copyright; $copyright = copyrightRange($years); ?>
We define our function, copyrightRange(), in functions.php like so:
function copyrightRange($years)
{
$cr = '';
if (sort($years)) {
if ($years[0] == $years[count($years) - 1])
$cr = $years[0];
else
$cr = $years[0] . ' – ' . $years[count($years) - 1];
}
return $cr;
}
All this function does is sort the array, then return either a single year or a range of years. We created this function so we could do the same thing in index.php. The only difference there is when we define the array, we pre-populate it with our _FIRST_YEAR_:
< ?php $years = array(_FIRST_YEAR_); ?>
Save all your template files, and you’re done. Now, your copyright notice on your home page reflects your entire blog, and other pages have notices appropriate for the posts on those pages.
The best part is, you won’t have to spend any more New Year’s days updating your blog’s copyright.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Copyright word AND symbol is too much. It equals “copyright copyright”.
On topic – personally I prefer simple edit once a year to having bunch of extra code around. I would probably use it if it was WordPress native stuff, but the less third party things the less is probability of something going wrong.
Ha! Yeah, you’re right about the “copyright copyright” thing. I see it that way so much that I just copied it without thinking about it.
This is all based on standard PHP and WP functions — none of which are going away anytime soon. You should always be a little wary of 3rd party code, but in this case I’m pretty confident it will hold up and not damage anything.
I agree with Rarst on the point of code vs. static HTML. Granted, with caching, it’s not a big deal, but ever little bit counts right?
Speed kills!
This is awesome. Though I have blogger blogs, but I may need this in future when I’ll shift to self hosted blog. Bookmarked…
Nice post. I like the code functions you are using for automatic insertion of the copyright years. I chose to do it manually, I just opened my footer.php and changed the dates accordingly. Is true that we can forget to do this but the lesser code the better, I hate to bloat my WordPress themes mainly because I’m not a PHP expert.
Thanks for all you do. Luis.
Thanks for the heads up.
But, I see a 2008 right there, in the footer, here !!
This is awesome, thanks! I think I’ll wait ’till I upgrade though.
This is really cool! thanks a lot for the heads up and for the valuable information.