3

I have a block in my footer that I currently use to display a copyright message using PHP, like so:

Copyright © <?php echo date("Y"); ?>

I'd like to add the site name to that code snippet, dynamically instead of typing the name. How can I do so?

So the end result would be:

Copyright © <?php echo date("Y"); ?> [[[php code to display site name goes here]]]

Thanks!

5

You can use variable_get() to get the site name, but you shouldn't use it directly in the template file or you could break caching.

Instead, define a preprocess function in your theme's template.php file:

function MYTHEME_preprocess_page(&$vars) {
  $vars['site_name'] = variable_get('site_name', 'Default');
}

Then you can use this in the template file:

Copyright © <?php echo date("Y"); ?> <?php echo $site_name; ?>

Before doing that, though, check whether you already have $site_name available in page.tpl.php. Some themes add it for you, some don't.

  • I followed those two steps, but it did not work. Instead, I received the following error message: " × Error message Notice: Undefined variable: site_name in eval() (line 1 of /home/mysite/public_html/modules/php/php.module(80) : eval()'d code)." – hockey2112 Jan 28 '14 at 18:12
  • My page.tpl.php file does have this: "<?php print $site_name; ?>". However, the suggestions above still do not work. I cleared cache, no effect. – hockey2112 Jan 28 '14 at 18:13
  • Did you replace "MYTHEME" with the name of your theme? – Clive Jan 28 '14 at 18:27
  • If this is in a block, then preprocessing the page won't do it, rather, you are going to have to put the variable_get()s inside it, and since the error is coming from the php module, this suggests you are doing this through the Block UI and using the php input filter that brings up all sorts of other nastiness I won't go into here... – Jimajamma Jan 28 '14 at 18:34
  • 1
    Putting a <?php print variable_get('site_name', 'Default'); ?> in there should do what you are wanting then. As for if it's recommended, I'd moreso suggest creating a block in a custom module instead as you can see the code, eg, it's not in the database, plus you have more control over it, eg, typos, plus any potential security issues. – Jimajamma Jan 28 '14 at 18:38
1

As an alternative to creating a pre-process function in your template, you can print the value of the site name variable in a block using this php snippet in your block

<?php
$site_name = variable_get('site_name');
echo $site_name;
?>

So, to print a copyright notice that includes your site name, it would look something like:

©copyright <?php echo date('Y');?><?php $site_name = variable_get('site_name'); echo "&nbsp"; echo $site_name; echo", "; ?> All Rights Reserved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.