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

first of all I apologize for my bad English. I hope you will correct me. ^_^

I have a form that has to be shown if there is NOT the cookie “cookie_srp”. I used this code:

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

To create the cookie use this js code:

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';
}

Everything works fine if the plugin “wp super cache” is disabled.
Instead if the plugin “wp super cache” is enabled the visualization will be wrong for everybody except the first visitor.

I found these solutions but do not know how to apply them, I dont know very well the ajax language.

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/

Related posts

Leave a Reply

1 comment

  1. Solved, This is a working e.g.

    <?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');
    ?>
    

    In function omni_ts_and_ref() { you can put the php code that will not be cached.