Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active February 2, 2018 18:32
Show Gist options
  • Save michaeluno/7356670 to your computer and use it in GitHub Desktop.
Save michaeluno/7356670 to your computer and use it in GitHub Desktop.
Demonstrates the use of the import field with Admin Page Framework v2.
<?php
/*
Plugin Name: Admin Page Framework - Import CSV
Plugin URI: http://en.michaeluno.jp/admin-page-framework
Description: Demonstrates the use of the import field.
Author: Michael Uno
Author URI: http://michaeluno.jp
Version: 0.0.2
Requirements: PHP 5.2.4 or above, WordPress 3.3 or above. Admin Page Framework v2.1.7b or above.
*/
if ( ! class_exists( 'AdminPageFramework' ) )
include_once( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/class/admin-page-framework.php' );
class APF_ImportCSV extends AdminPageFramework {
public function setUp() {
// Create the root menu
$this->setRootMenuPage( 'Tools' ); // specify the name of the page group
// Add the sub menus and the pages.
$this->addSubMenuPage(
__( 'CSV Import' ), // page title
'page_csv_import' // page slug
);
// Add form sections.
$this->addSettingSections(
array(
'strSectionID' => 'section_csv_import', // the section ID
'strPageSlug' => 'page_csv_import', // the page slug that the section belongs to
'strTitle' => __( 'CSV Import' ), // the section title
)
);
$this->addSettingFields(
array( // Sample text field
'strFieldID' => 'filed_sample_text',
'strSectionID' => 'section_csv_import',
'strTitle' => __( 'Sample Text' ),
'strType' => 'text',
),
array( // Import button
'strFieldID' => 'field_import_csv',
'strSectionID' => 'section_csv_import',
'strTitle' => __( 'Import' ),
'strType' => 'import',
'vLabel' => __( 'Import CSV File' ),
)
);
}
public function import_mime_types_page_csv_import( $arrMIMETypes ) { // import_mime_types_ + {page slug}
$arrMIMETypes[] = 'text/csv';
return $arrMIMETypes;
}
public function import_page_csv_import( $vData, $strFieldID ) { // import_ + {page slug}
// For debugging - uncomment the debug methods below to see what kind of data is passed.
// $this->oDebug->logArray( $strFieldID );
// $this->oDebug->logArray( $_POST );
// $this->oDebug->logArray( $_FILES );
// $this->oDebug->logArray( $vData );
// Do something with the imported data.
// $vData = ...
// For example, modify the value of the sample text filed.
$strFileName = ( isset( $_FILES['__import']['name']['field_import_csv'] ) )
? $_FILES['__import']['name']['field_import_csv']
: '';
$vData = array(
'page_csv_import' => array(
'section_csv_import' => array(
'filed_sample_text' => $strFileName
),
),
);
return $vData; // the returned data will be saved in the option.
}
}
// Instantiate the class object.
if ( is_admin() )
new APF_ImportCSV( 'APF_ImportCSV', __FILE__ );
@hhrealestatemedia
Copy link

Hi Michael,
Love the framework btw but I too can't upload a csv file. I've added $this->addSettingFields(
array(
'field_id' => 'my_section',
'type' => 'import',
'title' => 'Import CSV File',
),
to my public function setUp() { in my class APFDoc_UserMeta extends AdminPageFramework_UserMeta {

the field shows fine and i don't get any errors. When I upload, it says that the profile was updated fine. But there is nothing. The file isn't uploaded and the database isn't updated. Can you tell me where I'm going wrong here and also what I should expect to see after this file is uploaded? I'm hoping that the contents of the csv file will be added to a row in the wp_usermeta table under the correct user_id number. Please let me know what I'm dong wrong here or what I need to add to get this working. Thanks!
Justin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment