Dynamic Copyright
in Functions, Tips
as bns_dynamic_copyright, dynamic_copyright, get_posts, WordPress
At the bottom of most blogs you will find the commonplace copyright notice that may look something like this:
Copyright © 2010 WP First Aid All rights reserved.
Which begs the question, what about those posts made before the year 2010? This is where a dynamic copyright function would come in very handy. It would be more correct to have this display in the footer:
Copyright © 2009-2010 WP First Aid All rights reserved.
Most WordPress themes will include, at a minimum, code that will generate a current year copyright statement, perhaps something along these lines:
<?php _e( 'Copyright © ' ); ?> <?php echo date( 'Y' ); ?> <strong> <?php bloginfo( 'name' ); ?> </strong> <?php _e( 'All rights reserved.' ); ?>
To make this code into a function and add some dynamics lets start with a simple PHP construct:
function dynamic_copyright() {
// code goes here
}
To get the first year a post was written on the blog we need to find the first post. This can be done with this code snippet:
$all_posts = get_posts( 'order=ASC' ); $first_post = $all_posts[0]; $first_date = $first_post->post_date_gmt;
We started by getting all of the posts with the get_posts() function. We assigned the first post, which is found at index 0 (zero) to its own variable. Then we assign that post’s date (in GMT) to its own variable.
We now have isolated the date of the first post on the blog and can perform further operations on it. The basic dynamic part of the function is complete. If the first post is removed and the “new” first post has a different date, then the above snippet will simply use the new post’s date.
The balance of the function is only a slight modification of the original “minimum” sample code from above:
_e( 'Copyright © ' ); echo substr( $first_date, 0 ,4 ) . "-" . date( 'Y' ); echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> '; _e( 'All rights reserved.' );
The key is highlighted at line 6. We echo, or display, the first four characters of the $first_date string which is the year; add a hyphen; and, then call a standard PHP function to add the current year as a four digit representation.
Here is the entire dynamic_copyright() function, including comments:
function bns_dynamic_copyright() {
/* Get all posts */
$all_posts = get_posts( 'post_status=publish&order=ASC' );
/* Get first post */
$first_post = $all_posts[0];
/* Get date of first post */
$first_date = $first_post->post_date_gmt;
/* Display common footer copyright notice */
_e( 'Copyright © ' );
/* Display first post year and current year */
if ( substr( $first_date, 0, 4 ) == date( 'Y' ) ) {
/* Only display current year if no posts in previous years */
echo date( 'Y' );
} else {
echo substr( $first_date, 0, 4 ) . "-" . date( 'Y' );
}
/* Display blog name from 'General Settings' page */
echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> ';
_e( 'All rights reserved.' );
}
Of course, this function is compliments of BuyNowShop.com and has their customary bns prefix as it will be appearing in all of their themes.
Just like I did, you can copy and paste the entire sample bns_dynamic_copyright() code above into your theme’s functions.php file; then, replace the code in your theme’s footer.php file that generates your old static copyright notice with this line of code:
<?php bns_dynamic_copyright(); ?>
Your theme will now display a dynamic copyright notice as you continue your writings for many years to come. Congratulations!






[...] recently wrote a post covering the inclusion of this dynamic copyright function into most any theme. This added [...]
That’s an awful lot of code (and a hefty query – get_all_posts) to run on every single public-facing page, just to pull out the (static) starting year. Why not just hard-code it, since it’s not going to change (nobody is going back in time to write any blog posts, at least not from this side of the invention of the Flux Capacitor)? Then, just use PHP to display the current year.
Manually typing a short sentence or phrase is generally a lot less characters than creating a function to automate it, I agree. In most cases the year is “static” but not all cases, this function simply allows for that.
Perhaps there is a less intense method of accomplishing this goal; and, perhaps, instead of criticizing this one you could put forth another to accomplish the same goal?
My comment certainly wasn’t intended to criticize – just offer an alternative suggestion.
Here’s what I use:
© 2000-Well, crap. I’ll have to escape it:
© 2000-<?php echo date(Y); ?>
Yes, that works extremely well for a known quantity, as in the case of your blog (and your comfort level modifying your theme).
The key to the bns_dynamic_copyright function is the point of the start date not being a known quantity, or the same, for every installation of a theme using the function. The function allows the end user of these themes to simply have the information display, rather than editing the theme after every update.