Created
November 13, 2016 14:03
-
-
Save jonkpirateboy/63a981133da992f8605fa0054c64ac18 to your computer and use it in GitHub Desktop.
Import members from csv-file to custom post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: CUSTOMER import members | |
Plugin URI: http://www.CUSTOMER.com/ | |
Description: Import members from csv-file | |
Version: 1.0 | |
Author: Follow me Darling | |
Author URI: http://followmedarling.se | |
*/ | |
function CUSTOMERimportmembers_init() { | |
$plugin_dir = basename(dirname(__FILE__)); | |
load_plugin_textdomain( 'CUSTOMER-import-members', false, $plugin_dir . '/languages' ); | |
} | |
add_action('init', 'CUSTOMERimportmembers_init'); | |
function CUSTOMERimportmembers_css($hook) { | |
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('CUSTOMER-import-members.css', __FILE__) ); | |
} | |
add_action( 'admin_enqueue_scripts', 'CUSTOMERimportmembers_css' ); | |
function CUSTOMERimportmembers_menu() { | |
add_management_page(__('CUSTOMER import members', 'CUSTOMER-import-members'), __('CUSTOMER import members', 'CUSTOMER-import-members'), 'manage_options', 'CUSTOMERimportmembers_settings', 'CUSTOMERimportmembers_options'); | |
} | |
add_action('admin_menu', 'CUSTOMERimportmembers_menu'); | |
function CUSTOMERimportmembers_options() { | |
?> | |
<div class="wrap"> | |
<h2><?php _e('CUSTOMER import members', 'CUSTOMER-import-members'); ?></h2> | |
<form method="post" enctype="multipart/form-data"> | |
<table class="form-table"> | |
<tr valign="top"> | |
<th scope="row"> | |
<?php | |
_e('Choose file to upload', 'CUSTOMER-import-members'); | |
?> | |
</th> | |
<td> | |
<input type="file" name="fileToUpload" id="fileToUpload"> | |
</td> | |
</tr> | |
</table> | |
<div class="submit"> | |
<input type="submit" name="save_CUSTOMERimportmembers_settings" value="<?php _e('Import', 'CUSTOMER-import-members'); ?>" class="button-primary" /> | |
</div> | |
<?php | |
$plugin_dir = basename(dirname(__FILE__)); | |
$plugin_full_dir = WP_PLUGIN_DIR . '/' . $plugin_dir . '/'; | |
if( isset($_POST['save_CUSTOMERimportmembers_settings'] )) { | |
$row = 1; | |
$target_uploads = wp_upload_dir(); | |
$target_dir = $target_uploads['basedir']; | |
$target_file = $target_dir . '/' . basename( $_FILES["fileToUpload"]["name"] ); | |
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); | |
if (($handle = fopen($target_file, "r")) !== FALSE) { | |
$loop = new WP_Query( array( 'post_type' => 'members', 'posts_per_page' => -1 ) ); | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
wp_delete_post( get_the_ID() ); | |
endwhile; wp_reset_query(); | |
echo '<h3>' . __('Deleted old members', 'CUSTOMER-import-members') . '</h3>'; | |
echo '<div class="imported-members">'; | |
echo '<h3>' . __('Importing new members', 'CUSTOMER-import-members') . '</h3>'; | |
while (($data = fgetcsv($handle, 1000, " | |
")) !== FALSE) { | |
$num = count($data); | |
for ($c=0; $c < $num; $c++) { | |
if ($row > 1) { | |
$member = explode(';',$data[$c]); | |
$import_number = $row - 1; | |
echo '<div class="imported-member"><div class="imported-member-header">' . __('Imported member', 'CUSTOMER-import-members') . ' ' . $import_number . '</div><div class="ip">'; | |
$header = iconv("ISO-8859-1", "UTF-8", $member[0]); | |
$content = iconv("ISO-8859-1", "UTF-8", $member[1] . '<br>' . $member[2] . ' ' . $member[3] . '<br><a href="' . $member[4] . '" target="_blank">' . $member[4] . '</a>'); | |
echo '<strong>' . $header . '</strong><br>'; | |
echo $content; | |
$new_member = array( | |
'post_title' => $header, | |
'post_content' => $content, | |
'post_status' => 'publish', | |
'post_type' => 'members', | |
); | |
wp_insert_post( $new_member ); | |
echo '</div></div>'; | |
} | |
} | |
$row++; | |
} | |
echo '<div class="clear"></div>'; | |
echo '<h3>' . __('Done importing new members', 'CUSTOMER-import-members') . '</h3>'; | |
echo '</div>'; | |
fclose($handle); | |
} | |
unlink( $target_file ); | |
} | |
?> | |
</form> | |
</div> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment