Support » Plugins and Hacks » W3 Total Cache With Geolocation

  • My page speed programmer recently installed W3 Total Cache. And I’m sure that’s one of the reasons my page speed has improved.

    However there is one small glitch. My dynamic geolocation country shipping rate quote has started to malfunction sometimes. For example, I usually see my country’s rate. But other times, I see the U.S. or the default rate for ISP’s the geolocation program can’t read. Please see the red shipping rate quote in the footer, below the coupon here: http://www.comparingvitamins.com/iherb-coupon-code

    I found this thread about the same problem: http://wordpress.org/support/topic/plugin-w3-total-cache-how-to-cache-homepage-only?replies=2

    Can I edit either W3 Total Cache or my geolocation program (below) so that it gives the correct country quote always?

    Thank you,
    Greg

    ?php

    /* Plugin Name: Country X
    * Plugin URI: http://endemicdigital.com/
    * Description: Displays the country of the user and the shipping rate defined by client.
    * Author: Gary Henville
    * Version: 1.0
    * Author URI: http://www.endemicdigital.com/
    *
    * Copyright 2010 Gary Henville (begin@endemicdigital.com)
    *
    */

    // allow text widgets to use shortcodes
    add_filter(‘widget_text’, ‘do_shortcode’);

    // get user ip
    function get_ip_address() {
    foreach (array(‘HTTP_CLIENT_IP’, ‘HTTP_X_FORWARDED_FOR’, ‘HTTP_X_FORWARDED’, ‘HTTP_X_CLUSTER_CLIENT_IP’, ‘HTTP_FORWARDED_FOR’, ‘HTTP_FORWARDED’, ‘REMOTE_ADDR’) as $key) {

    if (array_key_exists($key, $_SERVER) === true) {
    foreach (explode(‘,’, $_SERVER[$key]) as $ip) {
    if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
    return $ip;
    }
    }
    }
    }
    }

    // get user country
    function get_country_x() {
    include_once(ABSPATH.”maxmind/geoip.inc”);

    $ip = get_ip_address();
    $gi = geoip_open(ABSPATH.”maxmind/GeoIP.dat”,GEOIP_STANDARD);

    $country = geoip_country_name_by_addr($gi, $ip);

    geoip_close($gi);

    return $country;
    }

    // display user country
    function view_country_x() {
    return ‘<div id=”country-shipping-x”>’.get_country_x().'</div>’;
    }

    // get shipping rate for country
    function get_shipping_rate_x() {
    include_once(“shipping-rate.php”);
    include_once(“country-groups.php”);

    $country = get_country_x();

    if (stristr($country_group_a, “|$country|”)) {
    return $country.’ ‘.$rate_a;
    } elseif (stristr($country_group_b, “|$country|”)) {
    return $country.’ ‘.$rate_b;
    } elseif (stristr($country_group_c, “|$country|”)) {
    return $country.’ ‘.$rate_c;
    } elseif (stristr($country_group_d, “|$country|”)) {
    return $country.’ ‘.$rate_d;
    } elseif (stristr($country_group_e, “|$country|”)) {
    return $country.’ ‘.$rate_e;
    } else {
    return $default_rate_text;
    }
    }

    // display shipping rate for country shortcode
    function view_shipping_rate_x($atts, $content=null) {
    extract(shortcode_atts(array(
    ‘ra’ => ”,
    ‘rb’ => ”,
    ‘rc’ => ”,
    ‘rd’ => ”,
    ‘re’ => ”
    ), $atts));

    include_once(“shipping-rate.php”);

    if ($ra == ”) {
    $ra = $rate_a;
    }

    if ($rb == ”) {
    $rb = $rate_b;
    }

    if ($rc == ”) {
    $rc = $rate_c;
    }

    if ($rd == ”) {
    $rd = $rate_d;
    }

    if ($re == ”) {
    $re = $rate_e;
    }

    if ($content == ”) {
    $content = $default_rate_text;
    }

    include_once(“country-groups.php”);

    $country = get_country_x();

    if (stristr($country_group_a, “|$country|”)) {
    return ‘<div id=”country-shipping-x”>’.$country.’ ‘.$ra.'</div>’;
    } elseif (stristr($country_group_b, “|$country|”)) {
    return ‘<div id=”country-shipping-x”>’.$country.’ ‘.$rb.'</div>’;
    } elseif (stristr($country_group_c, “|$country|”)) {
    return ‘<div id=”country-shipping-x”>’.$country.’ ‘.$rc.'</div>’;
    } elseif (stristr($country_group_d, “|$country|”)) {
    return ‘<div id=”country-shipping-x”>’.$country.’ ‘.$rd.'</div>’;
    } elseif (stristr($country_group_e, “|$country|”)) {
    return ‘<div id=”country-shipping-x”>’.$country.’ ‘.$re.'</div>’;
    } else {
    return ‘<div id=”country-shipping-x”>’.$content.'</div>’;
    }
    }

    // shortcode to display user country
    add_shortcode(‘country_x’, ‘view_country_x’);

    // shortcode to display shipping rate for user country
    add_shortcode(‘shipping_rate_x’, ‘view_shipping_rate_x’);

    ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Specifically, I wonder if it’s wise to change the settings or disable one type of cache (Page Cache?)

    I’m not familiar with how that geolocation plugin works, did you ask the author how that plugin works with caching plugins?

    Hi Frederick,

    Thanks for the reply. I didn’t ask him.

    I’ve noticed that my country’s shipping rate is correct 95% of the time. But the other 5% it shows the wrong country for me. Any ideas why?

    Again, I’m not familiar with that plugin, so i can’t guess.

    Again, I’m not familiar with that plugin, so i can’t guess.

    Think about it.

    The problem is not plugin-specific.
    ANY geo-specific results will be screwed up by ANY caching.
    That is, caching is MEANT to show current user same data as previous, without considering user location.

    The ability to configure caching to not show cached data except to one country users would be fantastic, and not that complex …

    You could try fragment caching to give you some dynamic functionality.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘W3 Total Cache With Geolocation’ is closed to new replies.