1

I wrote a plugin to add a csv file to the database. I created a custom table to use. My file path looks like this,

wuno-uploader/wuno-uploader.php
wuno-uploader/inventory.csv

On an settings option page in the admin dashboard I have a button. When the button is pressed and submitted it runs my code with no errors.

I console.log() after each part of my program to make sure everything is running. Everything run till I get to the part that I start to handle the csv file. I get no errors and the string at the end of the file does echo to the screen.

I am wondering if there is some type of Wordpress feature that is blocking me from being able to convert the file.

This is the full code in my plugin. This is the first plugin I have wrote so any feedback would be appreciated.

<?php
/**
 * Plugin Name: Wuno Uploader
 * Plugin URI: 
 * Description: This plugin adds functionality for uploading CSV files to the database
 * Version: 1.0.0
 * Author:
 * Author URI: 
 * License: Copyright (C) 
 * Unauthorized copying of this file, via any medium is strictly 
 * 
 * 
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

// create custom plugin settings menu
add_action('admin_menu', 'wuno_plugin_create_menu');

function wuno_plugin_create_menu() {

    //create new top-level menu
    add_menu_page('Wuno Plugin Settings', 'Wuno Installer', 'administrator', __FILE__, 'wuno_settings_page' , plugins_url('/images/icon.png', __FILE__) );

    //call register settings function
    add_action( 'admin_init', 'register_wuno_settings' );
}

function register_wuno_settings() {
    //register our settings
    register_setting( 'wuno-settings-group', 'file_to_install' );
    register_setting( 'wuno-settings-group', 'some_other_option' );
    register_setting( 'wuno-settings-group', 'option_etc' );
}

function wuno_settings_page() {
 if (isset($_POST['wuno-inventory'])) {
        productsExec();
    } 
?>

<h1>Wuno Inventory Updater</h1>

<form method="POST">
    <label for="wuno-inventory">Path To Inventory</label>
    <input type="text" name="wuno-inventory" id="wuno-inventory" value="inventory.csv">
    <input type="submit" value="Install" class="button button-primary button-large">
</form>

<?php
    } 

function productsExec() {
       require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
       global $wpdb;
       $table_name = $wpdb->prefix . "wuno_inventory";
       // path where your CSV file is located
       define('CSV_PATH','');
       // Name of your CSV file
       $csv_file = CSV_PATH . "inventory.csv"; 

       $sql = "DROP TABLE IF EXISTS $table_name";
       $wpdb->query($sql);

      $sql = "CREATE TABLE " . $table_name . " (
      id int(8) NOT NULL AUTO_INCREMENT,
      wuno_product varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
      wuno_description varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
      wuno_alternates varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
      wuno_onhand varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
      wuno_condition varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY  (id)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";

           $wpdb->query($sql);

    if (($handle = fopen($csv_file, "r")) !== FALSE) {
           fgetcsv($handle);   
           while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                for ($c=0; $c < $num; $c++) {
                  $col[$c] = $data[$c];
                }

         $col1 = $col[0];
         $col2 = $col[1];
         $col3 = $col[2];

        // SQL Query to insert data into DataBase
        $query = "INSERT INTO" . $table_name . "(wuno_product, wuno_description, wuno_alternates, wuno_onhand, wuno_condition) 
        VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."')";

        $results = $wpdb->query( $query );

        }
        fclose($handle);
        }
     echo "<h2>The inventory was successfully imported to the database!</h2>";     
 }

?>
3
  • There is the code review stack for such question, although I doubt they will have any good input there. This is a Q&A site and the question is not really a Q......You can ask people at the "the loop" chat to take a look, but here such a full code review is off topic. Feb 12, 2016 at 5:56
  • I am not asking for code review. I am asking why Wordpress would not let me enter data to a database with a function that clearly works elsewhere. I think it has to do with security. I am very aware of how SO works. I just figured I would venture over to wordpress.SE and see if someone here was more knowledgeable. about Wordpress specifically.
    – wuno
    Feb 12, 2016 at 6:01
  • 1
    Then please edit the question to make it clear what you are actually asking. "I am wondering if there is some type of Wordpress feature that is blocking me from being able to convert the file" is not realy a question, please specify exactly what is not working and relevant debug efforts. Feb 12, 2016 at 9:40

1 Answer 1

2

To start with, you need to fix the define of CSV_PATH:

define('CSV_PATH',dirname(__FILE__));

as without that, $handle will be false - no file opened or data read.

...then before you run it take another look becaue $col4 and $col5 are not set anywhere either...

3
  • I solved this last night at 4am. You are so darn right it was the path amongst a few other small errors. But can you in more detail explain this. I ended up fixing it like this, // path where your CSV file is located define('CSV_PATH', '../wp-content/plugins/wuno-uploader/'); When using dirname(FILE) instead what is the difference what is actually happening?
    – wuno
    Feb 12, 2016 at 17:08
  • When I change to the way you show me it does not file the file...
    – wuno
    Feb 12, 2016 at 17:13
  • dirname(__FILE__) will get the current directory of the plugin file (which works since your inventory.csv is in the same folder as the plugin). this is generally much better that setting a static path as it allows the directory name to be changed, but for a private plugin (not intending to be distributed) a static location is file so long as you remember that it is.
    – majick
    Feb 13, 2016 at 1:47

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.