First page of the dynamic_copyright archive.

Dynamic Copyright Revisited

Posted by The Doctor on Jun 13, 2011 with No Comments | Short Link
in Tips
as , , ,

A while back I wrote an article about a function to generate a dynamic copyright to display in themes.  Although I imagine the function can be adapted to be used elsewhere, the original intent was to place the function call in the footer of any given theme.

The function itself can be found in all the themes currently available from Cais at BuyNowShop.com via the WordPress Extend Themes repository. There have been a few minor changes to the function since it was introduced; and, it is just recently undergoing much more dramatic changes.

That being the case, I thought this would be a great opportunity to revisit the function and discuss what has changed since the last time I wrote about it here.

To see the BNS Dynamic Copyright code click here.To hide the BNS Dynamic Copyright code click here.

What’s changed:

  1. A `function_exists` conditional check has been added
  2. Better internationalization string formating has been applied.
  3. Default arguments have been set to generate a common notice.
  4. The `apply_filters` function has been implemented to allow for easier customization.

Also to note, the defaults are now written slightly different than the orginal generated copyright notice structure. The following are the current defaults:

  • ‘start’ => “Copyright”
  • ‘copy_years’ => the copyright symbol + the dynamically generated years with published posts
  • ‘url’ => points to the top level domain with the web site title as anchor text
  • ‘end’ => “All Rights Reserved.”

All of these paramters are now easily customized using the common WordPress structure, for example:

bns_dynamic_copyright( 'start=Mine, All Mine!&end=This means you, scrapers!' );

… would produce the following for this site (as of this writing):

Mine, All Mine! © 2009-2011 WP First Aid This means you, scrapers!

Of course, the above may not be appropriate for your own website … or maybe it is.

All the same, feel free to implement this updated function; and, don’t forget to look for it in the next releases of themes from BuyNowShop.com

Enjoy!

Dynamic Copyright

Posted by The Doctor on Feb 16, 2010 with 6 Comments | Short Link
Last modified by The Doctor on June 20, 2010
in Functions, Tips
as , , ,

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 &copy; ' ); ?>
<?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 &copy; ' );
  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 &copy; ' );
  /* 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!