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.

No PHP in a Drupal footer message

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 &#169; rather than the symbol to ensure correct output - see http://www.w3schools.com/tags/ref_entities.asp for further details):

<div id="footer">&#169; Copyright <?php echo date('Y'); ?><?php print $footer_message . $footer ?></div>

which results in:

PHP in a Drupal theme footer

Problem solved.

26 comments

Jeff Geerling May 17th, 2009 @ 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.

Guest May 17th, 2009 @ 7:05pm

What about adding token support to the footer field?

© Copyright [year]

Brian Puccio May 17th, 2009 @ 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 = 2006
print "<p>Copyright $startyear"
if ($currentyear > $year) print "-$currentyear"
print "</p>"
Blagojce May 17th, 2009 @ 10:31pm

Thanks, will use this code for my themes.

Laurence May 17th, 2009 @ 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
'&#169; Copyright ' . $startyear;
if (
$currentyear > $startyear) {
  echo
'-' . $currentyear;
}
?>
dalin May 18th, 2009 @ 6:37am

Better yet, use Creative Commons which will fix more than just your PHP problem.

Guest May 18th, 2009 @ 3:14pm

Nice! Who couldn't use this? Thank you.

Jeremy Epstein May 19th, 2009 @ 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:

  1. Go to 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:
    &copy; 2002-# MySite Inc.
  2. In your Drupal 6 theme's mytheme_preprocess_page() function (in template.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_message variable, 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.

Laurence May 19th, 2009 @ 3:16pm

@ Jeremy Epstein - nice solution! Thanks for posting that (one slight change I had to make in the case of Garland was using $vars instead of $variables in the theme's template.php file).

Also, great news about the footer message block :)

Adam Gregory June 3rd, 2009 @ 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.

Jordan Garn June 7th, 2009 @ 2:02am

straight and too the point, awesome! Helped me out thanks.

Guest July 20th, 2009 @ 11:26am

If I do this for 6.13 will it be easy to upgrade to 7.x?

Dave LePage July 30th, 2009 @ 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 &copy; ' . $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.

Laurence August 7th, 2009 @ 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!

drupal theming October 7th, 2009 @ 11:42am

very easy and useful , i am going to use it for my site.thanks for this again....

Dave - SearchEn... October 10th, 2009 @ 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

Jason Santini October 14th, 2009 @ 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 :)

Catherine Ellis October 24th, 2009 @ 7:00pm

Got this into a block and is working great

mschiff February 12th, 2010 @ 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:

<?php
print '<p class="copyright">' . sprintf($footer_message, date('Y')) . '</p>' . $footer
?>
Mike February 23rd, 2010 @ 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.”

Mike February 24th, 2010 @ 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.

Drupal Theme Garden March 7th, 2010 @ 8:36pm

Quite nice trick, thanks.

Kevin April 5th, 2010 @ 9:03pm

Thanks for the examples, we want to use a drupal blog rather than wp.

Oliver Davies May 4th, 2010 @ 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 &copy; <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!

Alaturka June 2nd, 2010 @ 1:48pm

Thanks you all, It helped me. I was changing the date each year :)

Running July 14th, 2010 @ 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.