-
-
Save michaeluno/7356670 to your computer and use it in GitHub Desktop.
<?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__ ); |
I haven't seen your last comment. Let me see if I can add a filter for it.
[Edit]
All right. Updated the above sample code and the branch v2.1.7b https://github.com/michaeluno/admin-page-framework/tree/2.1.7b. It has a new import_mime_types_{...}
filter so that you can add your custom allowed MIME types.
By the way, when I test it, .csv files seem to be recognized as 'application/octet-stream' though.
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
Well I tested it again and it still does not accept the csv file. There is an error, that the uploaded file type ist not supported.
I can deal with the file through the methods import_ but I can´t upload the file.
When I add 'text/csv' to line 2648 in the framework main class:
I get it work. But without it, no deal.