Website Templates Search

Dynamic create copyright date in Wordpress

  • Click to view live demo By Admin
  • Click to view live demo Oct 03, 2017

Generally you will see a website that has an outdated copyright date which is annoyning, but here we will provide solution for this.

Here is a simple php solution to this, that most developers must know.

In this article we will share you a function that will automatically return a copyright date based on the published date of your oldest and newest post.

Simple PHP Solution for Dynamic Copyright Date


    &copy; 2007– <?php echo date('Y'); ?> YourSite.com(e.g."http://www.phponweb.com/")

You would paste this simple PHP code in your theme’s“functions.php “file. But there is problem respectively to this code is that, you would have to add this  once your site is atleast one year old.

WordPress Solution for Dynamic Copyright Date

WordPress provides a graceful solution for dyanamic copyright date, suggested by“@frumph of CompicPress theme”.

This function will generate a dyanamic copyright date based on the published date of your oldest post and your newest post. If it is the first year of your site, then this function will only display the current year.

Just open your theme’s “functions.php” file and add the following code.


<?php

	function comicpress_copyright() {
	
	   global $wpdata;
	   $copyright_dates = $wpdata->get_results("
							   SELECT
								YEAR(min(post_date_gmt)) AS firstdate,
								YEAR(max(post_date_gmt)) AS lastdate
								FROM
								  $wpdata->posts
								WHERE
								  post_status = 'publish'
							   ");
							   
	  $output = '';
	  if($copyright_dates) {
	  
		  $copyright = "&copy; " . $copyright_dates[0]->firstdate;
		  
		  if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
		  
			  $copyright .= '-' . $copyright_dates[0]->lastdate;
			  
		   }
		   
		 $output = $copyright;
	   }
	   
	   return $output;
    }

?>		 
		 

open your “footer.php” file and add the following code where you want to display the date.


<?php 

      echo comicpress_copyright(); 
	  
?>

This function will add the following text

© 2017 – 2018

It is very helpful for your website and one thing remember that don’t keep your copyright dates outdated.