0
function cookiebasedredirect() {
?>
<script type="text/javascript">
    jQuery(document).on('click', '#organisation', function(event){
      event.preventDefault();
      jQuery.cookie("site_changer", "organisation", { expires: 1 });
      window.location.href = window.location.href;
    });

    jQuery(document).on('click', '#young-professional', function(event){
      event.preventDefault();
      jQuery.cookie("site_changer", "young-professional", { expires: 1 });
      window.location.href = window.location.href;
    });
  </script>
<?php

 }
 add_action("wp_head", "cookiebasedredirect");
  • Empty browser and caching plugin's caches. – Max Yudin Aug 9 '18 at 11:47
  • Cleared but still not working ... – Ravi Borasi Aug 9 '18 at 12:16
2

Your script uses jQuery so you should make sure that:

  • jQuery is enqueued on your site (it may be for logged-in users, especially admins, but not necessarily for logged-out users)
  • your script is added with jQuery as a dependency so jQuery is available when your script runs

I would use wp_add_inline_script() for this, like so:

function wpse310935_cookiebasedredirect() {
    ob_start();
    ?>
    jQuery(document).on('click', '#organisation', function(event){
      event.preventDefault();
      jQuery.cookie("site_changer", "organisation", { expires: 1 });
      window.location.href = window.location.href;
    });

    jQuery(document).on('click', '#young-professional', function(event){
      event.preventDefault();
      jQuery.cookie("site_changer", "young-professional", { expires: 1 });
      window.location.href = window.location.href;
    });
    <?php
    $my_js = ob_get_clean();
    if( ! wp_script_is( 'jquery', 'enqueued' ) ) {
        wp_enqueue_script( 'jquery' );
    }
    wp_add_inline_script( 'jquery', $my_js );
}
add_action( 'wp_enqueue_scripts', 'wpse310935_cookiebasedredirect' );
  • thanks for the support its working, now i am facing when click on inner pages it will have to change but it doesn't .please have a look elevateyp.com – Ravi Borasi Aug 9 '18 at 12:27
  • click on organisation then click inner pages . – Ravi Borasi Aug 9 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.