假设您可以在.csv
文件,您应该能够编写一个可以导入它的函数。我这样做是为了在客户端从现有站点或软件迁移时引入用户表和/或自定义帖子。下面是一些示例代码。您可以将其作为包含在函数中或作为插件的一部分运行,如果启动后不需要,则禁用它:
// always find line endings
ini_set(\'auto_detect_line_endings\', true);
// add admin menu
add_action(\'admin_menu\', \'csvimporter_menu\');
function csvimporter_menu() {
add_submenu_page( \'edit.php?post_type=physician\', \'CSV Importer\', \'Import\', \'manage_options\', \'csv-importer\', \'csvimporter\');
}
// show importer form
function csvimporter() {
global $wpdb;
if (!current_user_can(\'manage_options\')) {
wp_die( __(\'You do not have rights to access this page.\') );
}
// when form is submitted
if ($_POST[\'mode\'] == "submit") {
$arr_rows = file($_FILES[\'csv_file\'][\'tmp_name\']);
// process the rows
if (is_array($arr_rows)) {
foreach ($arr_rows as $row) {
// split into values -- note I prefer to separate by semicolon, you must setup your CSV to match this delimiter
$arr_values = split(";", $row);
// these will need rekeyed!! this is just an example!! In this example I build a long title from the physician\'s name
$name = $arr_values[0]; //fname
$name .= (empty($arr_values[1]) ? \'\' : \' \' .$arr_values[1]); //mname
$name .= (empty($arr_values[2]) ? \'\' : \' \' .$arr_values[2]); //lname
$name .= (empty($arr_values[3]) ? \'\' : \' \' .$arr_values[3]); //suffix
$name .= (empty($arr_values[4]) ? \'\' : \', \' .$arr_values[4]); //first degree
$name .= (empty($arr_values[5]) ? \'\' : \', \' .$arr_values[5]); //second degree
// add the new post
$arr_post = array( \'comment_status\' => \'closed\',
\'ping_status\' => \'closed\',
\'post_author\' => 1,
\'post_title\' => $name,
\'post_status\' => \'publish\',
\'post_type\' => \'physician\',
);
$post_id = wp_insert_post( $arr_post );
// add all the meta values
$arr_meta_values = array(
\'_cmb_last_name\' => $arr_values[2],
\'_cmb_board_1\' => $arr_values[6],
\'_cmb_board_2\' => $arr_values[7],
\'_cmb_group_name\' => $arr_values[11],
\'_cmb_address_1\' => $arr_values[12],
\'_cmb_address_2\' => $arr_values[13].(!empty($arr_values[14]) ? \', \'.$arr_values[14] : \'\'),
\'_cmb_city\' => $arr_values[15],
\'_cmb_state\' => $arr_values[16],
\'_cmb_zip\' => $arr_values[17],
\'_cmb_phone\' => $arr_values[18],
\'_cmb_status\' => $arr_values[19]
);
// fill up terms if you wish
$taxonomy = \'physician-specialty\';
$terms = array();
if(!empty($arr_values[8])){
// get ID if is term
$term_id = is_term($arr_values[8]);
if($term_id != 0){
// is term, add to list
$terms[] = $term_id;
} else {
// is not term, create, then add to list
wp_insert_term($arr_values[8], $taxonomy);
$term_id = is_term($arr_values[8]);
$terms[] = $term_id;
}
}
// assuming more term columns are there...
if(!empty($arr_values[9])){
$term_id = is_term($arr_values[9]);
if($term_id != 0){
$terms[] = $term_id;
} else {
wp_insert_term($arr_values[9], $taxonomy);
$term_id = is_term($arr_values[9]);
$terms[] = $term_id;
}
}
// add all terms at once
wp_set_post_terms($post_id, $terms, $taxonomy);
foreach($arr_meta_values as $key => $value) {
update_post_meta($post_id, $key, $value);
}
}
$html_update = "<div class=\'updated\'>It worked! I think.</div>";
} else {
$html_update = "<div class=\'updated\' style=\'color: red\'>Something went terribly wrong!</div>";
}
}
?>
<div class="wrap">
<?php echo $html_update; ?>
<div id="icon-plugins" class="icon32"><br /></div>
<h2>CSV Importer</h2>
<p>Select the CSV file you want to import</p>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" value="submit">
<input type="file" name="csv_file" />
<input type="submit" value="Import" />
</form>
<p>The CSV file should be in this sweet, sweet format. Oh yea!</p>
<table>
<tr>
<td>firstname;</td>
<td>middlename;</td>
<td>lastname;</td>
<td>blah;</td>
<td>etc;</td>
</tr>
</table>
<p style="color: red">Please make sure you back up your database before proceeding!</p>
</div>
<?php
}
?>
您需要确保
CSV
设置正确。我强烈建议备份数据库并只运行
sample 数据集(20行),以确保其正常工作。注意,我的示例仅对我自己的数据需求进行了抽样。最终结果可能包括更多
meta_keys
, 分类法内容、实际内容等。请确保为
CSV
. Excel可能不允许您设置
CSV
正如你所愿,我推荐OpenOffice。