Posts Tagged ‘copyright notice’

How do I add the date to my blog?

There are a lot of times when we want to add something as simple as the date to our blog but coding (especially in PHP) can be a bit difficult. Let me walk you through the steps to adding a date to your blog, assuming of course you use WordPress.

First, open your template files such as the footer by loading the footer.php file located in your /wp-content/themes/[theme name]/ folder.

Next, we need to insert a very simple PHP function called date() into your page but before we do that, we need to make sure that we open and close the PHP event. To do this, simply open it using <?php and close it using ?>. The date() function itself requires a formating argument, but luckily we’re not meant to remember them all. They can be found on the PHP date() reference page.

As an example, if you want to show the day of the week that it currently is you could open your website and edit it every day (highly impractical) or you could insert the code <?php echo date(‘l’); ?> into your page. Note that I open the PHP event, place the date() function with the argument ‘l’, indicate the end of a line (;) and then close the PHP event.  Each of these elements are required to properly format the date.

If you would like to show the current year, simply use: <?php echo date(‘Y’); ?> because Y is the symbol for the year. On the other hand, if you’d like to use something more complex, try <?php echo date(‘l jS \of F Y h:i:s A’); ?> which will return something like Monday 8th of August 2005 03:12:46 PM.

Once you’ve saved your footer.php file, simply re-upload it to your server and voila! Now you can set the date using your server and PHP instead of editing files every day! In more practical terms, it is how you can always have an updated copyright notice of your website without having to remember to update it each January.

Generating a Dynamic Copyright Notice in WordPress

Copyright notices are located in the footer of almost every website on the Internet and for good reason, it’s part of what protects us from content thieves or at least in theory but most of our copyright notices are outdated or hand coded, even in WordPress so how do we get around this?

The Current Year

My first solution was to place a small piece of PHP code in the footer of my WordPress file so that it always showed the current year, like so:

Copyright <?php echo date(“Y”);?> Christopher Ross.

Which resulting in:

Copyright 2009 Christopher Ross.

An Earlier Year to The Current Year

That system worked fine but was technically wrong since many of my posts predate 2009, which led me to my second solution of simply posting an earlier date in front of the PHP code like so:

Copyright 2005-<?php echo date(“Y”);?> Christopher Ross.

Still, technically wrong since I had no idea what the earliest post in my blog was and I’m too lazy to search through all the posts to find out. Which led me to the desire a script which would automatically find the first occurrence of a post in my blog and post the results as a copyright statement.

Automating the Copyright notice

To automate the copyright notice, first I needed to establish the first post as well as the last. The following code will accomplish that task:

 

function copyright() {

global $wpdb;

$posts = $wpdb->get_results(“SELECT YEAR(post_date_gmt) AS year FROM $wpdb->posts WHERE post_date_gmt > 1970 ORDER BY post_date_gmt ASC”);

$last = $posts[count($posts)-1]->year;

$first = $posts[0]->year;

 

$c = __(‘Copyright (&copy;) ‘, ‘inove’) . $first;

if($first != $last) {

    $c .= ‘-’. $last;

}

return $c;

}

Simply placing the function in your functions.php file will allow you to insert the code <?php echo  copyright();?> into your footer, resulting in a fully automated copyright notice as you can see on the footer of my website.

For those users who prefer to use a plugin, I’ve uploaded a WordPress plugin version of the copyright tool as well.

WordPress Plugin to automatically update the copyright notice.

What is this WordPress plugin?

This plugin automatically generates a copyright notice from the first post in your blog to the current year. The Auto Copyright notice plugin fetches the first and last year of posts from the database and displays the results as a copyright notice on your website.

This process allows blog owners to add content retroactively, knowing that the  website will automatically find the first date a post is published as well as the last published date or the current year (whichever is later).

To use the plugin, simply download it and add the code  <?php echo  autocopyright();?> to your website where you’d like the copyright notice to appear.

download file WordPress Plugin to automatically update the copyright notice. image

How You Can Help

I love developing plugins for WordPress and do my best to always keep them free but of course it take a lot of time and effort to build these plugins so if you’d like to say thanks, the best way is to take a few moments and write about the plugin on your own website, include a link to my website from your posts or download and rate the plugin on the official plugin directory.

Support forums are now online! If you have any questions, please visit http://forums.thisismyurl.com.