Wordpress,WP超级缓存:显示或隐藏一个元素,如果cookie存在


Wordpress, WP Super Cache: show or hide an element if cookie exist

首先,我为我的英语不好而道歉。我希望你能纠正我。^_^

我有一个表格,如果没有cookie"cookie_srp",则必须显示。我使用了以下代码:

if(!isset($_COOKIE['cookie_srp'])){
echo 'form code';
}  

要创建 cookie,请使用以下 js 代码:

function srp_cookie() {
  var date = new Date();
  date.setTime(date.getTime()+(srp_optime));
  var expires = "; expires="+date.toGMTString();
  document.cookie = 'cookie_srp=set;'+expires+"; path=/";
  document.getElementById('srp_slider').style.display = 'none';
}

如果禁用插件"wp 超级缓存",一切正常。相反,如果启用了插件"wp super cache",则除了第一个访问者之外,可视化对每个人都是错误的。

找到了这些解决方案,但不知道如何应用它们,我不太了解 ajax 语言。

http://omninoggin.com/wordpress-posts/make-any-plugin-work-with-wp-super-cache/http://z9.io/2008/11/01/make-your-wordpress-plugin-talk-ajax/

已解决,这是一个工作,例如

<?php
/*
Plugin Name: Hello World
Plugin URI: #
Description: A simple hello world plugin
Version: 0.1
Author: #
Author URI: #
*/
function omni_ts_and_ref() {
  echo time();
  echo '<br/>';
  echo $_POST['omni_ref'];
}
function omni_request_handler() {
  if ( isset($_POST['srp_action']) && $_POST['srp_action'] == 'srp_value' ) {
    omni_ts_and_ref();
    exit();
  }
}
add_action('init', 'omni_request_handler');
function omni_display() {
  echo '
    <div id="omni_div"></div>
    <script>
    jQuery(document).ready(function($){
     //document.cookie = "TR_LNG=something";
      if (document.cookie.indexOf("TR_LNG=") >= 0) {
            var co_test = "yes";
      }else{ 
            var co_test = "no"; 
      }
      $.ajax({
        type : "POST",
        url : "index.php",
        data : { srp_action : "srp_value", omni_ref : co_test },
        success : function(response){
          // the server has finished executing PHP and has returned something, so display it!
          $("#omni_div").html(response);
        }
      });
    });
    </script>
  ';
}
add_action('wp_footer', 'omni_display');
?>

在函数 omni_ts_and_ref() { 中,你可以放置不会被缓存的 php 代码。

相关文章: