Using PHP In Your Drupal Theme's Footer
The problem:
Drupal’s default 'Footer message' area (to which you can add content via the admin section - Administer > Site configuration > Site information) will not parse PHP.
If, for example, you add a copyright message to your footer message area such as "© Copyright 2009" it'll be fine until the New Year begins and you have to go in and manually change it to "© Copyright 2010". So, instead, it would be great if you could handle the date dynamically using the following PHP code (see http://php.net/date for further PHP date details):
© Copyright <?php echo date('Y'); ?>However, try adding that to the default 'Footer message' area and it will simply output the entirety of the code as text, ignoring the PHP tags.

So, what to do?
Solution 1:
Create a block (admin/build/block/add) containing the code, set its input format to PHP, and then place that block in the footer region via the main blocks settings page (admin/build/block). Crazy simple.
Please note - you will need to ensure that you have the 'PHP filter' module enabled. This is not enabled by default in Drupal 6, but is one of the 'Core - optional' modules and can easily be enabled via your site's admin modules section (admin/build/modules).
Solution 2:
Insert the code, containing the PHP, directly into your Drupal theme's page.tpl.php file. To do so, first open your theme's page.tpl.php file and locate the footer output, which in Garland looks something like this:
<div id="footer"><?php print $footer_message . $footer ?></div>Please note - there are actually two things being output here:
- $footer_message - which is the output from Drupal's admin 'Footer message' area (admin/settings/site-information)
- $footer - which is the actual footer region (to which you can add blocks via the main blocks settings page - admin/build/block)
Then simply insert your code wherever you'd like the copyright message to appear, for example (in this case we use the copyright symbol's html entity number © rather than the symbol to ensure correct output - see http://www.w3schools.com/tags/ref_entities.asp for further details):
<div id="footer">© Copyright <?php echo date('Y'); ?><?php print $footer_message . $footer ?></div>which results in:

Problem solved.



26 comments
@ 4:27pm
It would be nice if there were a module that would enable PHP, or at least that snippet, in the footer field in the site information page; it's not easy for a regular site content manager to FTP into the server, load up the page.tpl.php file, and edit the source code! The block solution is okay, but if Drupal has a footer area on the site information page, it should be a little more powerful, methinks.
@ 7:05pm
What about adding token support to the footer field?
© Copyright [year]
@ 7:57pm
I'm not legal expert, but if you're site has been around for several years, say since 2006, your copyright should say "Copyright 2006-2009" or the like. And it is considerably better to have the a copyright that ranges from 2006-2009 than just 2009 alone. So instead, you'd need (and excuse the psuedo code):
$startyear = 2006print "<p>Copyright $startyear"
if ($currentyear > $year) print "-$currentyear"
print "</p>"
@ 10:31pm
Thanks, will use this code for my themes.
@ 11:14pm
Hi all, thanks for the comments.
@ Jeff Geerling - yes a module would be cool.
@ Brian Puccio - thanks for the suggestion, although your code is just a little off. The following will work for what you want to achieve:
<?php$startyear = 2006; //replace 2006 with your actual start year
$currentyear = date('Y');
echo '© Copyright ' . $startyear;
if ($currentyear > $startyear) {
echo '-' . $currentyear;
}
?>
@ 6:37am
Better yet, use Creative Commons which will fix more than just your PHP problem.
@ 3:14pm
Nice! Who couldn't use this? Thank you.
@ 6:20am
I've been using a simple, token-esque solution for a long time (actually, I wrote it long before the token module was available). You can see it in action in my own site's footer. Here's how I do it:
administer > site configuration > site information, and add a#character to your site's footer message (or, potentially, add some other character(s) of your choosing), eg:© 2002-# MySite Inc.mytheme_preprocess_page()function (intemplate.php), add this:<?php$variables['footer_message'] = str_replace('#', date('Y'), $variables['footer_message']);
?>
Also note that in Drupal 7, the footer message is a block and there is no more
$footer_messagevariable, so this will no longer be an issue. You'll simply be able to add the PHP filter to your footer block's input format, and to implement Laurence's solution #1; and you'll also be able to use the token module.@ 3:16pm
@ Jeremy Epstein - nice solution! Thanks for posting that (one slight change I had to make in the case of Garland was using
$varsinstead of$variablesin the theme's template.php file).Also, great news about the footer message block :)
@ 7:22pm
To answer one of the issues above about regular site managers being able to access the theme files, use the Theme Editor Module and you can grant access to some of the theme files for them to edit.
@ 2:02am
straight and too the point, awesome! Helped me out thanks.
@ 11:26am
If I do this for 6.13 will it be easy to upgrade to 7.x?
@ 9:50pm
You could also include the site title, so it's clear who the copyright belongs to:
<div id="copyright"><?php
$startyear = 2006; //replace 2006 with your actual start year
$currentyear = date('Y');
echo 'Copyright © ' . $startyear;
if ($currentyear > $startyear) {
echo '-' . $currentyear;
}
?>
<?php print $site_title ?>
</div>
That way if you make changes to the name of the site later, you would not have to touch the theme.
@ 12:58pm
Hi everyone, thanks for the new comments. And apologies for the delayed reply - I've been on vacation :)
@ Guest - Drupal 7 will include a footer message block (see comment #8 - @ Jeremy Epstein), so it'll be easier to use that instead. All you should need to do is move the code into the block and set its input format to 'PHP code'.
@ Dave LePage - nice idea!
@ 11:42am
very easy and useful , i am going to use it for my site.thanks for this again....
@ 9:30am
We typically ignore the footer message area altogether and we often put the boilerplate and/or copyright message right into the page template.
We also use the footer region solution in a lot of cases where we want it to be editable by clients.
I've always kinda wondered about the point of putting a copyright message in place, after all, original work is supposed to be copyright to the author/s automatically, and certainly, having a copyright message ain't going to stop anyone copying (or heavily plagiarizing) your content...
Just a thought, maybe there are good arguments for it.
- Dave
@ 2:49am
I love PHP! Thank you for this !! and Brian thank you for the comment about the 2006-2009 thing - this also works in Wordpress too - with a couple of minor tweaks ex.Step one :)
@ 7:00pm
Got this into a block and is working great
@ 7:14pm
I just put a printf compatible format string into the footer message, for example:
© 2000-%d My Company Inc.
And then in the template use:
<?phpprint '<p class="copyright">' . sprintf($footer_message, date('Y')) . '</p>' . $footer
?>
@ 11:11pm
#16
You can check the United States Copyright Office for more information. Namely, check circular number 3.
Extract: "Use of the notice may be important because it informs the public that the work is protected by copyright, identifies the copyright owner, and shows the year of first publication. Furthermore, in the event that a work is infringed, if the work carries a proper notice, the court will not give any weight to a defendant’s interposition of an innocent infringement defense—that is, that he or she did not realize that the work was protected. An innocent infringement defense may
result in a reduction in damages that the copyright owner would otherwise receive.
" For works first published on and after March 1, 1989, use of the copyright notice is optional. Before March 1, 1989, the use of the notice was mandatory on all published works. Omitting the notice on any work first published before that date could result in the loss of copyright protection if corrective steps are not taken within a certain amount of time. The curative steps are described in this circular under “Omission of Notice and Errors in Notice.”
@ 3:37am
Re #16 & Re#26/
I just noticed that this blog is written from someone outside the U.S. / so the reference to the U.S. Copyright Office is somewhat irrelevant.
The issue with the copyright notice is that copyright law is mostly territorial, except for a number of international agreements into place... (see WIPO, etc.) if I am not mistaken, each country still has the right to either limit or increase the amount of damages that can be claimed, or the affirmative defenses that may be considered or excluded (innocent infringement, etc.), based on whether or not the copyright notice was present. So yes, it is still wise to add it.
@ 8:36pm
Quite nice trick, thanks.
@ 9:03pm
Thanks for the examples, we want to use a drupal blog rather than wp.
@ 12:19pm
After seeing the comments about including the Site Name in the footer, I've done this as well as including a link back to the homepage and the standard footer_message variable.
<?php$startyear = 2006; //replace 2006 with your actual start year
$currentyear = date('Y');
echo 'Copyright © <a href="' . $front_page . '">' . $site_name . '</a>' . ', ' . $startyear;
if ($currentyear > $startyear) {
echo '-' . $currentyear;
}
print '. ' . $footer_message;
?>
I'm sure that I could clean this up a bit, but it works!
@ 1:48pm
Thanks you all, It helped me. I was changing the date each year :)
@ 10:06pm
Thanks for the tip. I'm just starting out with Drupal and it's the small building blocks like this that are making the journey bearable - thank for sharing.
Comments are closed
If you want to ask a question or have something to add please contact me.