Created
October 8, 2020 05:40
-
-
Save devlifeX/2ab6cde36900ea1a21229eba9464a643 to your computer and use it in GitHub Desktop.
Import wordpress taxonomy, You can aslo override exported taxonomy
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 | |
class Import_Taxonomy | |
{ | |
private $options = []; | |
public function __construct($options) | |
{ | |
if ( | |
isset($_GET['task']) && | |
$_GET['task'] === "import" | |
) { | |
$this->options = $options; | |
add_action('init', [$this, 'doAction']); | |
} | |
} | |
public function doAction() | |
{ | |
$items = json_decode($this->options['content'], true); | |
$override_taxonomy = null; | |
$is_override_taxonomy = false; | |
if ( | |
isset($this->options['override_taxonomy']) && | |
!empty($this->options['override_taxonomy']) | |
) { | |
$override_taxonomy = $this->options['override_taxonomy']; | |
$is_override_taxonomy = true; | |
} | |
foreach ($items as $key => $item) { | |
$name = $item['name']; | |
$taxonomy = $is_override_taxonomy ? $override_taxonomy : $item['taxonomy']; | |
unset($item['term_id']); | |
unset($item['name']); | |
unset($item['taxonomy']); | |
wp_insert_term( | |
$name, | |
$taxonomy, | |
$item | |
); | |
} | |
print_r("Taxonomy import DONE!"); | |
exit(); | |
} | |
} | |
$filename = "tax.json"; | |
$content = file_get_contents(__DIR__ . "/" . $filename); | |
new Import_Taxonomy(['content' => $content, 'override_taxonomy' => 'listingcategory']); | |
/** | |
* Run like this: http://localhost/?task=import | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment