Created
August 18, 2024 11:14
-
-
Save WebSofter/64035009f0e3fb7037e05a47d961d0f0 to your computer and use it in GitHub Desktop.
Hierarchical import into wordpress custom taxonomies from structured txt file
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
#1 Авто и мото | avtomoto | |
АЗС | azs | |
Автобизнес | avtobizness | |
Автосалоны | avtosalon | |
Автосервисы | avtoservis | |
Автострахование | avtostrahovanie | |
Автофорумы и блоги | avtoforum | |
Автохимия | avtohim | |
Автошколы | avtoshkola | |
Аренда автомобилей | avtoarenda | |
Безопасность и сигнализации | bezopasnostisignal | |
Грузовики | gruzavto | |
Запчасти и аксесуары | avtozapchasti | |
Купля-продажа | avtoprodazha | |
Мойки | moikaavto | |
Официальные дилеры | avtodiler | |
Перевозка и доставка | perevozkaidostavka | |
Производители | proizvodstvoavto | |
Разное | avtoraznoe | |
Спецтехника | spectehnika | |
Такси | taxi | |
Транспорт | avtotransport | |
Тюнинг | tuningavto | |
#2 Бизнес и финансы | biznesfinansi | |
Аналитика | analitika | |
Ассоциации | associacii | |
Банки | bank | |
Биржи | birzha | |
Бухгалтерский учет | buhuchet | |
Инвестиции | invest | |
Инвестиционные компании | investkompaniya | |
Ипотека | ipoteka | |
Консалтинг | konsalt | |
Курсы валют и акций | kursvalut | |
Маркетинг | marketing | |
Микрозаймы | microzaim | |
Недвижимость | nedvizhimost | |
Работа | rabotabiz | |
Разное | raznoebiznes | |
Реклама | reklamabiz | |
Финансовые издания | finizdaniya | |
Фирмы и компании | firms | |
Фонды | fondi | |
Электронная коммерция | komers |
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
function import_tax_from_file($file = 'rubrics.txt', $taxonomy = 'articles') { | |
// Define the path to the file | |
$file_path = get_template_directory() . '/'.$file; // Adjust the path as needed | |
// Open the file for reading ('r' mode) | |
if ( file_exists( $file_path ) && is_readable( $file_path ) ) { | |
$file_handle = fopen( $file_path, 'r' ); | |
// Check if the file was opened successfully | |
if ( $file_handle ) { | |
// Read the file line by line | |
while ( ( $line = fgets( $file_handle ) ) !== false ) { | |
// Process the line (e.g., output or manipulate the data) | |
if(preg_match('/[#]/', $line)) { | |
$line = trim(preg_replace("/[#\d]/", "", $line)); | |
$ns = explode("|", $line); | |
$name = trim($ns[0]); | |
$slug = trim($ns[1]); | |
// echo esc_html( 'name:'.$name ) . '<br>'; | |
// echo esc_html( 'slug:'.$slug ) . '<br>'; | |
$parent_term = wp_insert_term( | |
$name, // The name of the parent term | |
$taxonomy, // The taxonomy slug | |
array( | |
'slug' => $slug, | |
) | |
); | |
} else if(!empty(trim($line))) { | |
$ns = explode("|", $line); | |
$name = trim($ns[0]); | |
$slug = trim($ns[1]); | |
// echo esc_html( '-name:'.$name ) . '<br>'; | |
// echo esc_html( '-slug:'.$slug ) . '<br>'; | |
if ( ! is_wp_error( $parent_term ) ) { | |
wp_insert_term( | |
$name, | |
$taxonomy, | |
array( | |
'parent' => $parent_term['term_id'], | |
'slug' => $slug, | |
) | |
); | |
} | |
} | |
// | |
} | |
// Close the file handle after reading | |
fclose( $file_handle ); | |
} else { | |
echo 'Unable to open the file.'; | |
} | |
} else { | |
echo 'File does not exist or is not readable.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment