Change SITE depending on incoming domain - header_required.php override?

Permalink
Hi There,

We've overridden header_required.php and are looking for a way to change the value of SITE depending on the incoming domain.

We've created our own variable and omitted SITE altogether (which works) like this - from line 28 in header_required.php
$domain = $_SERVER['HTTP_HOST'];
if (strpos($domain, 'www.website.com.au') || strpos($domain, 'website-oz.dev.com') !== FALSE) {
  $siteName = 'Website Name Australia';
} else {
  $siteName = 'Website Name';
}
$pageTitle = sprintf(PAGE_TITLE_FORMAT, $siteName, $pageTitle);
// Default c5 output commented out
// $pageTitle = sprintf(PAGE_TITLE_FORMAT, SITE, $pageTitle);

However, I'm unsure if SITE is used elsewhere so would prefer to modify the value of SITE if possible. Does anyone know how to do that – or is there a better way?

Any help or pointers in the right direction would be much appreciated.

Cheers

Ben

 
Mainio replied on at Permalink Best Answer Reply
Mainio
A better way is always to avoid any core overrides. You can actually do this also on the theme level where you include the header_required element. This is further explained in our Multiple Domain add-on's documentation:
https://www.concrete5.org/marketplace/addons/multiple-domains/docume...

You can apply the same logic in your situation, although the section of the documentation is targeted towards users of Multiple Domains (but you can see the idea from there).

As to your other question, the usage of the SITE constant depends usually on the theme. It might be used also e.g. in the footer, if there is a copyright statement there or maybe other places as well (sidebars etc.). Depends very much on the theme.

An easy way to find out where it is used would be to do a global search against your full code base and see it yourself.
cmscss replied on at Permalink Reply
Thanks heaps for the reply,

I've since discovered that my code above wasn't working as expected - I will check out your link.

Dumb question, but can I just define SITE in the switch statements we're using for our different environments like this?
case 'www.website.com' :
      define('DB_SERVER', 'localhost');
      define('DB_USERNAME', 'username');
      define('DB_PASSWORD', 'password');
      define('DB_DATABASE', 'database');
      define('SITE', 'Website Name');
      break;
Mainio replied on at Permalink Reply
Mainio
Yes, you sure can do that as well!
cmscss replied on at Permalink Reply
Thanks for that, unfortunately it appears that if you use the bulk SEO uploader, then the site name won't output at all.

Will look at your links.
Mainio replied on at Permalink Reply
Mainio
Yes, that's true. If you have the "meta_title" attribute set for the pages, the whole default page title is ignored.

The alternative method suggested by the Multiple Domains documentation (the link above) would work regardless of that. It looks like the manually passed $pageTitle will always win if it's set.
cmscss replied on at Permalink Reply
Thanks Mainio,

Ended up using a combination - for my future reference:

1. /config/site.php Define SITE constant depending on incoming domain:
// ----------------------- ENVIRONMENTS ----------------------------------------------------
// Loads correct database based on incoming domain
// Put live site first
switch ($_SERVER['HTTP_HOST']) {
  // DOMAIN ONE
  case 'domainone.com' :
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', 'database-user');
    define('DB_PASSWORD', 'database-password');
    define('DB_DATABASE', 'database-name');
    define('SITE', 'Site One Page Name');
  break;
  // DOMAIN TWO
  case 'domaintwo.com' :
    define('DB_SERVER', 'localhost');


2. /config/site.php Stop default SITE from outputting:
// ----------------------- FORMAT PAGE TITLES ----------------------------------------------------
Stop default site name from outputting in page titles
//https://www.concrete5.org/marketplace/addons/multiple-domains/docume...
define('PAGE_TITLE_FORMAT', '%2$s');


3. /themes/theme_name/elements/header.php Replace header required with modified call
<?php // define website name depending on incoming domain (also see site.php)
  // grab site name (site name defined in site.php - step 1 above)
  $siteName = SITE;
  // grab page meta title (use collection name as a fallback)
  $pageTitle = $c->getCollectionAttributeValue('meta_title'); // grab meta title
  if (empty($pageTitle)) {
    $pageTitle = $c->getCollectionName(); // use collection name as a fallback
  }
  // output header required along with title contraining page meat title and site name 
  Loader::element('header_required', array('pageTitle' => sprintf('%2$s – %1$s', $siteName, $pageTitle)));
?>