About design and nearby

How to Add a Dynamic PHP Copyright Notice in WordPress

Posted: August 21st, 2014 | Author: | Filed under: Development, Tips and Tricks | Tags: , , , | No Comments »

It’s pretty simple to display a dynamic date in your WordPress site (I do that into the footer area) usually, you just need to add a line of code to your theme (according to WordPress Codex):

  1. <?php the_date( $format, $before, $after, $echo ); ?>

How to add a dynamic copyright date

So simple php solution in our case to print the current year the code is

  1. <?php echo date('Y'); ?>

You can modify a little the copyright notice by adding your website’s start year:

  1. &copy; Copyright 2009 <?php echo date('Y'); ?> All rights reserved

Or even more – start with the website name:

  1. <?php bloginfo('name'); ?> &copy; Copyright 2009 <?php echo date('Y'); ?> All rights reserved

Once I found more elegant way to generate a dynamic copyright date based on the published date of your earliest and latest posts (suggested by Philip M. Hofer (Frumph) for a ComicPress WP theme). If your site is less than a [current] year old, then this function will display the current year only.

To implement the above feature, add to your function.php file:

  1. function my_copyright() {
  2. global $wpdb;
  3. $copyright_dates = $wpdb->get_results("
  4. SELECT
  5. YEAR(min(post_date_gmt)) AS firstdate,
  6. YEAR(max(post_date_gmt)) AS lastdate
  7. FROM
  8. $wpdb->posts
  9. WHERE
  10. post_status = 'publish'
  11. ");
  12. $output = '';
  13. if($copyright_dates) {
  14. $copyright = "&copy; " . $copyright_dates[0]->firstdate;
  15. if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
  16. $copyright .= '-' . $copyright_dates[0]->lastdate;
  17. }
  18. $output = $copyright;
  19. }
  20. return $output;
  21. }

and then call to the function where you want to display the date:

  1. <?php bloginfo('name'); ?> <?php echo my_copyright(); ?> All rights reserved

And you’re done!

Share Button

Check Related Posts:


Leave a Reply


  • 1 − = zero

Looking for a Freelance UX & UI designer for your project? I’m ready to jump onboard!

Let's discuss your project now