title: Insert some posts in a loop - wordpress plugin
body: <p>I wrote a plugin, which reads csv files and create new products. The plugin works when I create only one product but when I add <code> while </code> in <code>Insert()</code> the plugin doesn't work. I want to create all products first off. Maybe it's something related to the add_action... Please help.</p>
  
  <pre><code>define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
  
  function CreateProduct($line) {    
      $data = explode('";"', $line);  
      define(POST_NAME, $data[2]);
      define(cena_netto_pw, $data[5]);
  
      $post = get_page_by_title( POST_NAME, 'OBJECT', 'product'  );
      $product_ID = $post-&gt;ID;
      $post_data = get_post($product_ID);
  
      function hbt_create_post() {
          $my_post = array(
            'post_title'    =&gt; POST_NAME,
            'post_content'  =&gt; '',
            'post_status'   =&gt; 'publish',
            'post_author'   =&gt; 1,
            'post_type'     =&gt;'product'
          );
  
          $product_ID = wp_insert_post( $my_post );
      }
  
      if(!isset($post))
          hbt_create_post();
      return $error_obj;
  }
  
  function Import() {
      $file = PLUGIN_DIR.'test.csv';
      $open = fopen($file, 'r');
      while (!feof($open)) {
  
          $line = fgets($open);
          CreateProduct($line);
  
      }
      fclose($open); 
  }
  
  add_action('admin_init', 'Import' ); 
  
  ?&gt;
  </code></pre>
  
  <p>While loop code</p>
  
  <pre><code>while (!feof($open)) { $line = fgets($open); CreateProduct($line); }
  </code></pre>
  
  <p>This code doesn't work. It works when there is only </p>
  
  <pre><code>$line = fgets($open); CreateProduct($line);
  </code></pre>