In my Wordpress theme I have a function that looks a bit like this:
function variable($value) {
switch ($value) {
case 'prem_no':
$prem_no_uk = '0906 636 4355';
$prem_no_aus = '';
$prem_no_us = '';
return $prem_no;
break;
}
}
On the page I am calling:
echo variable('prem_no');
I want to use this script to get the users location, and depending on the returning location, return one of the three variables listed in the case above.
http://www.hostip.info/use.html
the problem I have, is I've looked on that site, and it just gives a URL. I don't know how to write an IF statement, or whatever is needed.
I think logically, I want to only have one call for the main location finder, then store that value as a variable elsewhere on the site.
Then in the return for the cases add a suffix at the end of prem_no that would match the cases above.
Is that along the right tracks?
UPDATE:
Right I'll try and have a go in a slightly different direction.
At the top of the header file I've got this:
require_once 'geoip.inc';
$gi = geoip_open('GeoIP.dat',GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
I'm getting a error message saying:
Warning: fopen(GeoIP.dat): failed to open stream: No such file or directory in C:\wamp\www\clairvoyant\wp-content\themes\clairvoyant\geoip.inc on line 314
What I don't get is that the path is absolutely correct, the file is there. So why can't it find it?
Okay, so I've managed to partly fix my issues here.
I put the switch and the code into a single function like this:
function variable($value) {
$country_code = '';
require_once("geoip.inc");
$gi = geoip_open(dirname(__FILE__)."/GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
switch ($value) {
case 'prem_no':
if ($country_code == 'US') { $prem_no = '888-888-8888'; }
elseif ($country_code == 'AU') { $prem_no = '1900 000 000'; }
else { $prem_no = '0906 636 4355'; }
return $prem_no;
break;
case 'prem_rate':
if ($country_code == 'US') { $prem_rate = '$3.50'; }
elseif ($country_code == 'AUS') { $prem_rate = '$3.96'; }
else { $prem_rate = '£1.53'; }
return $prem_rate;
break;
case 'local_no':
if ($country_code == 'US') { $local_no = '755-555-5555'; }
elseif ($country_code == 'AUS') { $local_no = '1800 000 000'; }
else { $local_no = '0207 111 6311'; }
return $local_no;
break;
case 'sms_no':
if($country_code == 'AUS') { $sms_no = '1977 1977'; }
else { $sms_no = '78887'; }
return $sms_no;
break;
case 'sms_rate':
if($country_code == 'AUS') { $sms_rate = '25c'; }
else { $sms_rate = '£1.50'; }
return $sms_rate;
break;
case 'helpline':
if($country_code == 'US') { $helpline = '700-777-7777'; }
elseif ($country_code == 'AUS') { $helpline = '1700 000 000'; }
else { $helpline = '0207 111 6210'; }
return $helpline;
break;
default:
break;
}
}
The file GeoIP.dat loads perfectly fine on every normal page, except when looking at the blog pages. (I'm assuming it must read functions differently or something? The error I get when trying to view the Blog loop articles is:
Warning: fopen(GeoIP.dat): failed to open stream: No such file or directory in C:\wamp\www\clairvoyant\wp-content\themes\clairvoyant\geoip.inc on line 314
then at the bottom of the trace table it says
Can not open GeoIP.dat
I just can't work out why it's working fine for normal pages, but not for blogs. Can anyone shed any light?
UPDATE:
It turns out Wordpress uses different pages for blog and normal pages. So I just needed to edit the other pages to match the initial ones, and it all seems to be working now.
Thanks for all of those who took the time to read my questions, sorry if it wasn't too clear or logical. But thanks anyway. All is good!
Related
I'm new to prestashop and I'm having major trouble removing the address(I want to have only Summary=Shrnutí, Login/Guest Checkout=Přihlásit se, Delivery=Doručení and Payment=Platba here https://www.enakupak.cz/objednavka?step=1) step,. I am using prestashop 1.6.1.5
I know I have to modify order-carrier.tpl file and have followed several posts here and there but couldn't get it done right.
Does any of you have any actual idea on how to do this ?
I think that it will be change in this part of OrderController.php but dont know how to concretly change it
switch ((int)$this->step) {
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
case OrderController::STEP_DELIVERY:
if (Tools::isSubmit('processAddress')) {
$this->processAddress();
}
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
break;
case OrderController::STEP_PAYMENT:
// Check that the conditions (so active) were accepted by the customer
$cgv = Tools::getValue('cgv') || $this->context->cookie->check_cgv;
if ($is_advanced_payment_api === false && Configuration::get('PS_CONDITIONS')
&& (!Validate::isBool($cgv) || $cgv == false)) {
Tools::redirect('index.php?controller=order&step=2');
}
if ($is_advanced_payment_api === false) {
Context::getContext()->cookie->check_cgv = true;
}
// Check the delivery option is set
if ($this->context->cart->isVirtualCart() === false) {
if (!Tools::getValue('delivery_option') && !Tools::getValue('id_carrier') && !$this->context->cart->delivery_option && !$this->context->cart->id_carrier) {
Tools::redirect('index.php?controller=order&step=2');
} elseif (!Tools::getValue('id_carrier') && !$this->context->cart->id_carrier) {
$deliveries_options = Tools::getValue('delivery_option');
if (!$deliveries_options) {
$deliveries_options = $this->context->cart->delivery_option;
}
foreach ($deliveries_options as $delivery_option) {
if (empty($delivery_option)) {
Tools::redirect('index.php?controller=order&step=2');
}
}
}
}
$this->autoStep();
// Bypass payment step if total is 0
if (($id_order = $this->_checkFreeOrder()) && $id_order) {
if ($this->context->customer->is_guest) {
$order = new Order((int)$id_order);
$email = $this->context->customer->email;
$this->context->customer->mylogout(); // If guest we clear the cookie for security reason
Tools::redirect('index.php?controller=guest-tracking&id_order='.urlencode($order->reference).'&email='.urlencode($email));
} else {
Tools::redirect('index.php?controller=history');
}
}
$this->_assignPayment();
if ($is_advanced_payment_api === true) {
$this->_assignAddress();
}
// assign some informations to display cart
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'order-payment.tpl');
break;
default:
$this->_assignSummaryInformations();
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
}
What if you cann this code after first case - break:
case OrderController::STEP_SUMMARY_EMPTY_CART:
$this->context->smarty->assign('empty', 1);
$this->setTemplate(_PS_THEME_DIR_.'shopping-cart.tpl');
break;
After this case add this case:
case OrderController::STEP_ADDRESSES:
$this->_assignAddress();
$this->processAddressFormat();
if (Tools::getValue('multi-shipping') == 1) {
$this->_assignSummaryInformations();
$this->context->smarty->assign('product_list', $this->context->cart->getProducts());
$this->setTemplate(_PS_THEME_DIR_.'order-address-multishipping.tpl');
} else {
$this->autoStep();
$this->_assignCarrier();
$this->setTemplate(_PS_THEME_DIR_.'order-carrier.tpl');
}
break;
Check, is it work.
I'm working on implementing some geoIP functionality to redirect a user away from my .com site to the relevant country domain (.fr, .es, .co.uk ...etc).
I've the following in my index.php to check the users IP:
ini_set('display_errors', 1);
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if($country_code == 'ES')
{
header('Location: https://www.testsite.es');
}
elseif($country_code == 'GB')
{
header('Location: https://www.testsite.co.uk');
}
elseif($country_code == 'FR')
{
header('Location: https://www.testsite.fr');
}
else {
header('Location: https://www.testsite.com/home');
}
When I check the $country_code variable it is an empty String and as a result the above fails and I always hit https://www.testsite.com/home...
I started delving into the code and noticed that first I call this method:
function geoip_country_code_by_addr($gi, $addr) {
if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
$record = geoip_record_by_addr($gi, $addr);
if ($record !== false) {
return $record->country_code;
}
} else {
$country_id = geoip_country_id_by_addr($gi, $addr);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_CODES[$country_id];
}
}
return false;
}
which calls:
function geoip_country_id_by_addr($gi, $addr) {
$ipnum = ip2long($addr);
return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
}
I can't figure out why it keeps failing and returning a '0'? I am using Maxminds geoip.inc php to check the country code.
I've checked that mbstring is enabled within my php.ini file and it is. For some reason it just doesn't find the Country code based on the IP I pass to it. Does anyone have any help in terms of what might be causing this?
just wanted to say that I've resolved the issue. A mistake on my part and probably a sign that I need a break!
Within geoip.inc.php supplied by Maxmind I was initially getting these errors:
Cannot redeclare geoip_country_code_by_name() in geoip.inc on line 438
In order to fix this I simply check if the method is defined and if not I use it as follows:
if (!function_exists('geoip_country_code_by_name')) {
function geoip_country_code_by_name($gi, $name) {
$country_id = geoip_country_id_by_name($gi, $name);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_CODES[$country_id];
}
return false;
}
}
I unfortunately had a minor typo in the above code which prevented the code from executing properly hence returing 0 each and every time.
I've got this function in my Wordpress theme directory:
function variable($value) {
$country_code = '';
require_once("geoip.inc");
$gi = geoip_open(dirname(__FILE__)."/GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
switch ($value) {
case 'prem_no':
if ($country_code == 'US') { $prem_no = '888-888-8888'; }
elseif ($country_code == 'AU') { $prem_no = '1900 000 000'; }
else { $prem_no = '0906 636 4355'; }
return $prem_no;
break;
case 'prem_rate':
if ($country_code == 'US') { $prem_rate = '$3.50'; }
elseif ($country_code == 'AUS') { $prem_rate = '$3.96'; }
else { $prem_rate = '£1.53'; }
return $prem_rate;
break;
case 'local_no':
if ($country_code == 'US') { $local_no = '755-555-5555'; }
elseif ($country_code == 'AUS') { $local_no = '1800 000 000'; }
else { $local_no = '0207 111 6311'; }
return $local_no;
break;
case 'sms_no':
if($country_code == 'AUS') { $sms_no = '1977 1977'; }
else { $sms_no = '78887'; }
return $sms_no;
break;
case 'sms_rate':
if($country_code == 'AUS') { $sms_rate = '25c'; }
else { $sms_rate = '£1.50'; }
return $sms_rate;
break;
case 'helpline':
if($country_code == 'US') { $helpline = '700-777-7777'; }
elseif ($country_code == 'AUS') { $helpline = '1700 000 000'; }
else { $helpline = '0207 111 6210'; }
return $helpline;
break;
default:
break;
}
}
This works fine for normal pages, and loads all the files listed above, and the switch works fine. However, when I go to view the blog articles I just get a blank page with the error message:
Warning: fopen(GeoIP.dat): failed to open stream: No such file or directory in C:\wamp\www\clairvoyant\wp-content\themes\clairvoyant\geoip.inc on line 314
and I can't work out why it would be different for normal pages and blog pages.
I know there are individual PHP files, but they all use the same functions file, right?
It's the GeoIP.dat it can't find, and it was doing this before last week before I added dirname(FILE) just before the filename.
The only difference I can think of, is my permalinks have a custom structure of /blog/%postname% - could the /blog be affecting the path somehow?
Any ideas as to why it's not working as expected just on blog pages?
UPDATE:
This is relevant function inside geoip.inc:
function geoip_open($filename, $flags) {
$gi = new GeoIP;
$gi->flags = $flags;
if ($gi->flags & GEOIP_SHARED_MEMORY) {
$gi->shmid = #shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
} else {
$gi->filehandle = fopen($filename,"rb") or die( "Oops! Can not open $filename\n" );
if ($gi->flags & GEOIP_MEMORY_CACHE) {
$s_array = fstat($gi->filehandle);
$gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
}
}
$gi = _setup_segments($gi);
return $gi;
}
BUMP: Can anyone throw some light on this? Wordpress seems to be stripping the path of $filename when viewing blog pages. When viewing normal pages, the full path is included.
I finally found out what was creating the different output between pages.
The wordpress blog pages were being generated using a separate index.php file than the other pages - this was down to the way the template I'm using worked.
On the blog index.php template, the filename was actually being hardcoded into the index file.
Removed that, and now just the function is being used in both, all is well!
Thanks to all for your help
EDIT - I have completely re-written the logic below but I'm sure it can still be improved.
EDIT - The current working code for the alternate theme selector is at GitHub. I'd still like to improve on it but it's a lot cleaner than what I started with. At this point, I'll take the issue thread there since user testing will reveal the next steps.
New Logic:
<?php
// Fetch theme options array
global $ats_plugin;
global $is_IE;
// Initialize the main variables
$ats_active = 0;
$the_theme = null;
// We only want to run this fat chunk of logic on IE since it's IE with the issues
if ( $is_IE ) {
// Is a supported useragent active?
function checkActive($data) {
global $ats_active;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
switch($user_agent) {
case strpos($user_agent, 'MSIE 5') :
if ($data['ie5_switch'] == 1 && !empty($data['ie5_theme'])) {
$ats_active = 'ie5';
};
break;
case strpos($user_agent, 'MSIE 6') :
if ($data['ie6_switch'] == 1 && !empty($data['ie6_theme'])) {
$ats_active = 'ie6';
};
break;
case strpos($user_agent, 'MSIE 7') :
if ($data['ie7_switch'] == 1 && !empty($data['ie7_theme'])) {
$ats_active = 'ie7';
};
break;
case strpos($user_agent, 'MSIE 8') :
if ($data['ie8_switch'] == 1 && !empty($data['ie8_theme'])) {
$ats_active = 'ie8';
};
break;
default :
$ats_active = 0;
}
// Dev Mode Overide
if($data['dev_mode'] == 1) {
$ats_active = 1;
}
return $ats_active;
}
// Run active agents check
checkActive($ats_plugin);
if(!empty($ats_active)) {
function change_theme() {
global $ats_plugin;
global $ats_active;
global $the_theme;
if(!empty($ats_plugin['ie5_theme'])) {$ie5 = $ats_plugin['ie5_theme'];} else {$ie5 = null;}
if(!empty($ats_plugin['ie6_theme'])) {$ie6 = $ats_plugin['ie6_theme'];} else {$ie6 = null;}
if(!empty($ats_plugin['ie7_theme'])) {$ie7 = $ats_plugin['ie7_theme'];} else {$ie7 = null;}
if(!empty($ats_plugin['ie8_theme'])) {$ie8 = $ats_plugin['ie8_theme'];} else {$ie8 = null;}
$theme_key = array(
'ie5' => $ie5,
'ie6' => $ie6,
'ie7' => $ie7,
'ie8' => $ie8,
);
// Only one value should return
foreach ($theme_key as $browser => $selection ) {
if ($ats_active == $browser) {
$the_theme = $selection;
}
}
// Add the dev mode override
if(!empty($ats_plugin['dev_theme'])) {
$the_theme = $ats_plugin['dev_theme'];
}
return $the_theme;
}
add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');
}
}
// For non-IE browsers, we check if the user is an admin and enable developer mode
if ( !$is_IE && current_user_can('manage_options') ) {
if($ats_plugin['dev_mode'] == 1 && !empty($ats_plugin['dev_theme'])) {
$the_theme = $ats_plugin['dev_theme'];
}
function dev_theme() {
global $the_theme;
return $the_theme;
}
/* #todo if the theme is in developer mode, there should be a visual warning as a reminder */
if ($ats_plugin['dev_mode'] == 1) {
add_filter('template', 'dev_theme');
add_filter('option_template', 'dev_theme');
add_filter('option_stylesheet', 'dev_theme');
}
}
I've been staring at this for too long... it's completely inefficient. I'm literally running the entire switch statement once, then checking if active is flagged, then I'm running it again. That's so very wrong. I desperately need a bit of perspective on the right way to structure the logic.
Background Information:
It's switching the theme based on a user agent as configured in a WP Plugin admin. The theme has a dev mode for configuring the plugin and an active mode for the user agents.
$ats_plugin grabs the Redux array
$the_theme grabs the selected theme from that array
$active grabs the switch for whether or not a theme is being served to that user agent.
The whole mess is being filtered back into WordPress on the end.
Here's my mess of code. It works but it's horribly inefficient.
Original Madhouse Logic:
<?php
/**
* Define some early logic, which should probably be moved to a class later
*/
// Initialize the main variables
$active = 0;
$the_theme = null;
function swapTheme()
{
// Fetch the plugin options array
global $ats_plugin;
global $the_theme;
global $active;
// Fetch the user agent
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Assign variables based on user agent
switch ($user_agent) {
case strpos($user_agent, 'MSIE 5') :
$active = $ats_plugin['ie5_switch'];
$the_theme = $ats_plugin['ie5_theme'];
break;
case strpos($user_agent, 'MSIE 6') :
$active = $ats_plugin['ie6_switch'];
$the_theme = $ats_plugin['ie6_theme'];
break;
case strpos($user_agent, 'MSIE 7') :
$active = $ats_plugin['ie7_switch'];
$the_theme = $ats_plugin['ie7_theme'];
break;
case strpos($user_agent, 'MSIE 8') :
$active = $ats_plugin['ie8_switch'];
$the_theme = $ats_plugin['ie8_theme'];
break;
default :
// Check for Developer Mode
if ($ats_plugin['dev_mode'] == 1) {
$active = 1;
// Check for Developer Theme
if (!empty($ats_plugin['dev_theme']) && current_user_can('manage_options')) {
$the_theme = $ats_plugin['dev_theme'];
} else {
$the_theme = null;
}
}
break;
}
return $the_theme;
}
swapTheme();
if (!empty($the_theme) && $active == 1) {
add_filter('template', 'swapTheme');
add_filter('option_template', 'swapTheme');
add_filter('option_stylesheet', 'swapTheme');
}
I'm putting my super humble student goggles on right now.
Screenshots of what I'm doing
OK, first of all, do not run swapTheme() outside of any filter. Second, just check if you already matched something and than simple return.
function swapTheme() {
if ( ! empty( $the_theme ) ) {
return;
}
// ...
}
While it's not exactly the answer I was hoping for, further iteration and a bit of banter with the guys at Redux Framework got things into a working state. The Alternate Theme Switcher code is now at Github if you'd like to see how I resolved the myriad issues that came up. I'd still like to further refactor things going forward but the question of "how to fix broken?" no longer needs to be active here.
Thanks for the input that was provided.
I am using latest version of TCPDF inorder to generate my PDF files and it is working fine
the problem where I stuck is I want to display file attachment annotation pane by default as it opens when user clicks on attachment image ...
I have tried the following :
$pdf->SetDisplayMode($zoom, $layout, $mode='UseAttachments');
but its not working.
Tell me how it is possible please ....
In my version of TCPDF, SetDisplayMode function looks like this:
public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
$this->ZoomMode = $zoom;
} else {
$this->Error('Incorrect zoom display mode: '.$zoom);
}
switch ($layout) {
//layout is set here
}
//page mode
switch ($mode) {
case 'UseNone': {
$this->PageMode = 'UseNone';
break;
}
case 'UseOutlines': {
$this->PageMode = 'UseOutlines';
break;
}
case 'UseThumbs': {
$this->PageMode = 'UseThumbs';
break;
}
case 'FullScreen': {
$this->PageMode = 'FullScreen';
break;
}
case 'UseOC': {
$this->PageMode = 'UseOC';
break;
}
case '': {
$this->PageMode = 'UseAttachments';
break;
}
default: {
$this->PageMode = 'UseNone';
}
}
}
Take a look at the last case. It will set a PageMode to "UseAttachments", but it looks to be defined in wrong way. Instead of case 'UseAttachments': there is case '':.
So, try to change your code to this:
$pdf->SetDisplayMode($zoom, $layout, '');