Last active
January 21, 2020 18:40
-
-
Save shaneonabike/e6005c268d1cb82b3a94fea1379e2ce7 to your computer and use it in GitHub Desktop.
Wordpress Inject Countries
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 | |
function insert_countries() { | |
wp_insert_term('CA', 'countries', array('description'=>'Canada')); | |
$array_data = csv_to_array('/var/www/cinema/wp-content/themes/cp-theme/country.csv'); | |
// Knock off the first line | |
array_shift($array_data); | |
foreach($array_data as $data) | |
{ | |
echo 'short:'.$data[0]; | |
echo 'long:'.$data[1].'<br>'; | |
if(!empty($data)) | |
{ | |
//Replace with your desired taxonomy. | |
$term_exists = term_exists($data[0], 'countries'); | |
if ($term_exists !== 0 && $term_exists !== null) { | |
echo $term_exists->name." exists!"; | |
}else { | |
$term = wp_insert_term($data[0], 'countries', array('description'=>$data[1])); | |
echo $term->name." inserted Successfully <br>"; | |
} | |
} | |
} | |
} | |
add_action('init','insert_countries'); | |
// function to convert csv data into array | |
function csv_to_array($filename='', $delimiter=',') | |
{ | |
if(!file_exists($filename) || !is_readable($filename)) {echo 'file issues'; | |
return FALSE; | |
} | |
$header = NULL; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== FALSE) | |
{ | |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) | |
{ | |
array_push($data,$row); | |
} | |
fclose($handle); | |
} | |
return $data; | |
// return array_flatten($data); | |
} | |
/** | |
* Convert a multi-dimensional array into a single-dimensional array. | |
* @author Sean Cannon, LitmusBox.com | seanc@litmusbox.com | |
* @param array $array The multi-dimensional array. | |
* @return array | |
*/ | |
function array_flatten($array) { | |
if (!is_array($array)) { | |
return false; | |
} | |
$result = array(); | |
foreach ($array as $key => $value) { | |
if (is_array($value)) { | |
$result = array_merge($result, array_flatten($value)); | |
} else { | |
$result = array_merge($result, array($key => $value)); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment