1

What I'm trying to do: If anyone who is not logged in, reads an article, I will increase a session var by one. Save that article ID via global $post and save the $post->ID to an array. After 3 articles I will show a login or register form. So I want to limit the free access to the articles to three. The 3 already watched articles should be able to be read anyway.

The main part of the code:

    function cta_main($content){   
   global $post;
   if(!is_user_logged_in()){
    if(!isset($_SESSION['last_posts'])){
      $_SESSION['last_posts']=array();
      $_SESSION['count']=1;
      $_SESSION['restriction']=false;
    }
    echo "POST_SIZE: ".sizeOf($post);
   echo "<br />Count: ".$_SESSION['count'];
    if(is_singular() && !$_SESSION['restriction'] && is_main_query()){
      if(!cta_isInFreeToWatch()){
      //array_push($_SESSION['last_posts'],$post->post_title);
          $_SESSION['count']++;
          $_SESSION['last_posts'][]=$post->ID;

      }     
      if(sizeOf($_SESSION['last_posts'])>=3)
      $_SESSION['restriction']=true;
    }  
  }  
  echo "<br /> verbot: ".$_SESSION['restriction'];
  echo "<br /> anzahl: ".sizeOf($_SESSION['last_posts']);
  echo "<br /> aktuell: ".$post->ID;
  echo "<br />";
  print_r($_SESSION['last_posts']);  
      if($_SESSION['restriction'])
          return  cta_get_the_restricted_area_message();

      if(!$_SESSION['restriction'] || cta_isInFreeToWatch())
        return $content;
 }

The Problem: First article-> Everything is fine. So the article id is in the array. Second article-> now it's weird, after loading the page the array has 3 or more ids in it. I don't understand why because I ask for is_single() but it runs multiple times. With every click it adds multiple ids to the array. Why? I'm sure it's something about the loop but I can't find a solution for that. I think before loading the entire page the function is called multiple times. (add_filter('the_content','cta_main'); that's when the function is called).

  • browser pre-fetch? – Milo Aug 1 '16 at 17:22
  • if so, how can i prevent that. or how can i find out? – sibbih Aug 2 '16 at 8:39

Your Answer

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

Browse other questions tagged or ask your own question.