So what I'm wanting to do is have my drupal site use one theme during the daytime and one theme during the night. I figure that I should be able to write a php if statement to switch the theme at certain times of the day. Where would I find the code that specifies which theme is default or active so that I may stick that conditional statement in there? Many thanks for any help!
Alright, got it all figured out for anyone that is interested, let me know if you have any questions.
The last bit of code is the only thing that you would need to edit to make it your own. You would change the database connect setting and you would replace the 'VARIABLE' with what is in your database under the "variable" table.
Also, I placed this in my cron.php file located in the root of my site. My cron is set to run every 5 minutes, so my theme SHOULD switch within about 5 minutes of the sunrise and sunset, pretty cool.
//Get's the IP adress from visitor to determine location with
if (getenv(HTTP_X_FORWARDED_FOR)) {
$pip = getenv(HTTP_X_FORWARDED_FOR);
$ip = getenv(REMOTE_ADDR);
} else {
$ip = getenv(REMOTE_ADDR);
}
//Processes the IP addresses and returns an array of data
$url = "http://www.ipmango.com/api.php?ip=".$ip;
$xml = simplexml_load_file($url);
/*echo "IP address : {$xml->ipaddress} <br />";
echo "City : {$xml->city} <br />";
echo "Region : {$xml->region} <br />";
echo "Country Name : {$xml->countryname} <br />";
echo "Latitude : {$xml->latitude} <br />";
echo "Longitude : {$xml->longitude} <br />";*/
//Rounding and storing data in variables
$longitude = round($xml->longitude, 2);
$latitude = round($xml->latitude, 2);
//Determine the GMT offset from longitude and latitude vars
$url = "http://ws.geonames.org/timezone?lat=".$latitude."&lng=".$longitude;
$xml = simplexml_load_file($url);
//Print the array of information generated from xml
//print_r($xml);
// Save the calculated offset in a var
$gmtoffset = round($xml->timezone->rawOffset);
//Setting the server default timezone to GMT
date_default_timezone_set("GMT");
//Define our variable for what time it is right now for each location
$now = (date("H") + date("i") / 60 + date("s") / 3600) + $gmtoffset;
//Correct for negative hour value
if ($now <= '0') {
$now+='24';
}
else {
}
//Setting the $sunrise variable equal to the sunrise at the users location
$sunrise = date_sunrise(time(), SUNFUNCS_RET_DOUBLE, $latitude, $longitude, 90, $gmtoffset);
//Setting the $sunset variable equal to the sunset at the user location
$sunset = date_sunset(time(), SUNFUNCS_RET_DOUBLE, $latitude, $longitude, 90, $gmtoffset);
//And the final touch
if (($now >= $sunrise) && ($now <= $sunset)) {
mysql_connect("DB_SERVER", "DB_USER", "DB_PASSWORD") or die(mysql_error());
mysql_select_db("DB_NAME") or die(mysql_error());
$result = mysql_query("UPDATE variable SET value='VARIABLE' WHERE name='theme_default'")
or die(mysql_error());
} else {
mysql_connect("DB SERVER", "DB_USER", "DB_PASSWORD") or die(mysql_error());
mysql_select_db("DB_NAME") or die(mysql_error());
$result = mysql_query("UPDATE variable SET value='VAR";' WHERE name='theme_default'")
or die(mysql_error());
}
Comments
It is not that much difficult
Changing theme programmatically is not that much difficult task. You just need to change the value of global $custom_theme to the name of them you wish to use. For that you can code it in hook_init() of one of your custom modules.
For Drupal 7, implement hook_custom_theme().