Process uploaded file in WordPress plugin development

I’m building my first WP plugin. Basically I should upload a CSV file and then read it values to store in a table at WordPress DB. After read a lot of docs I follow this steps:

  • Create two files frequent-traveler.php where all the logic resides and ft_admin.php where the view part is.
  • This is the code I have in frequent-traveler.php:

// Create database tables and some others
function frequent_traveler_activation()
{

}

register_activation_hook(__FILE__, 'frequent_traveler_activation');

// Deletes database tables and some others
function frequent_traveler_deactivation()
{

}

register_deactivation_hook(__FILE__, 'frequent_traveler_deactivation');

function frequent_traveler_admin_actions()
{
    add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
}

add_action('admin_menu', 'frequent_traveler_admin_actions');

function frequent_traveler_admin()
{
    include('ft_admin.php');
}

if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
    echo "entre";
}
else {
    echo "Error";
}

  • And this is the code at ft_admin.php:

<?php
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

?>

<div class="wrap">
    <?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
    <form name="frequent_traveler_form" method="post" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
        <input type="hidden" name="frequent_traveler_hidden" value="Y">
        <?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
        <p>
            <?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5"> 
            <?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
            <?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
        </p>
        <input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
    </form>
    <hr/>
    <?php ?>
    <h2><?php echo __('Upload file to import') ?></h2>
    <form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']) . '/uploadfile'; ?>" accept-charset="utf-8" >
        <?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
        <p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
            <input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>"  />
    </form>
</div>

<script>
    jQuery(document).ready(function() {
        jQuery('.datepicker').datepicker({
            dateFormat: 'dd/mm/yy'
        });
    });
</script>

As you may notice I’m trying to validate the uploaded file with this code:

Read More
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
    echo "entre";
}
else {
    echo "Error";
}

But I get always Error, why is that? What I’m doing wrong? Which is the best way to handle uploaded files?

Related posts

Leave a Reply

1 comment

  1. You should not leave the check for if (!empty($_POST) outside any function, it can go inside the admin_menu output.

    Also, the form actions should be left empty, so it posts to the same plugin page.

    The targeted enqueue can be done in a standard way too and note the shortcut for jQuery $:

    add_action( 'admin_menu', 'frequent_traveler_admin_actions' );
    add_action( 'admin_enqueue_scripts', 'admin_enqueue' );
    
    function frequent_traveler_admin_actions()
    {
        $my_hook = add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
        //var_dump($my_hook); die();
    }
    
    function admin_enqueue( $hook )
    {
        if( 'settings_page_ftconfig' !== $hook )
            return;
    
        wp_enqueue_script('jquery-ui-datepicker');
        wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
    }
    
    function frequent_traveler_admin()
    {
        if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
            echo "<h1>entre</h1>";
        }
        else {
            echo "<h1>Error</h1>";
        }
    
        $ft_default = '';
        $frequent_traveler_from_date = '';
        $frequent_traveler_to_date = '';
        ?>
    
        <div class="wrap">
            <?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
            <form name="frequent_traveler_form" method="post" action="">
                <input type="hidden" name="frequent_traveler_hidden" value="Y">
                <?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
                <p>
                    <?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5"> 
                    <?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
                    <?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
                </p>
                <input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
            </form>
            <hr/>
            <?php ?>
            <h2><?php echo __('Upload file to import') ?></h2>
            <form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
                <?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
                <p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
                    <input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>"  />
            </form>
        </div>
        <script>
          jQuery(document).ready(function($) {
            $('.datepicker').datepicker({
                dateFormat: 'dd/mm/yy'
            });
          });
          </script>
        <?php
    }