Programming, WordPress

PHP code to Import the taxonomy terms from one website to another

Are you trying to import taxonomy terms from one website to another website ?

1. Create a folder in the root directory of the site i.e PARENT_SITE from where we want to export the Taxonomy Terms. I have created services folder in the root directory and created the php file named books-category.php

2. Include wp-load.php the files so that we could use all the WordPress available functions

3. Write a code that echo’ s JSON representation of a terms related to book taxonomy . We can replace the taxonomy name by any other taxonomy we want to import

$terms = get_terms( array(
    'taxonomy' => 'books',
    'hide_empty' => false,
) );

$bookTerms =[];
foreach ($terms as $term) {
    $parentTerm = $term->parent;
	/*
	 * if the term has a parent
	 *
	*/
    if(!empty($term->parent) && $term->parent!=0) {
        $parentTermArray = get_term_by('id', $term->parent, 'books');
        $parentTerm = $parentTermArray->parent;
    }
    $bookTerms[] =[
        'slug'=> $term->slug,
        'name'=>$term->name,
        'term_id'=>$term->term_id,
        'parent_term_id'=> $parentTerm
    ];
};

$jsonPost[]= array(
    'post_book_terms'=> $bookTerms,
);
echo json_encode($jsonPost);

 

We can check the encoded taxonomy terms in the Url : http://PARENT_SITE/services/books-category.php

4. Create a folder in the root directory of the site I call it as CHILD_SITE, where we want to import the Taxonomy Terms.

I have created app folder in the root directory of CHILD_SITE and created the php file named import-books-taxonomy.php :

5. Create two meta fields to store the parent id and the id of taxonomy terms OF the PARENT_SITE in the same taxonomy of CHILD_SITE .

We can use acf to create the meta/custom field in the taxonomy : https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

6. We can Use Guzzle to parse the json response from the MAIN_SITE : http://docs.guzzlephp.org/en/stable/overview.html

7. Write a code to read the JSON formated terms and insert terms in database

require __DIR__.'/../wp-load.php';
require 'vendor/autoload.php';

$importTaxonomyUrl = "http://PARENT_SITE/services/books-category.php";
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $importTaxonomyUrl);
$items = json_decode($response->getBody());

foreach ($items as $item):
    if(!empty($item->post_book_terms)):
        foreach($item->post_book_terms as $postBookTag) {
            getTermsID($postBookTag,'books');
        }
    endif;
endforeach;


function getTermsID($termArray,$taxonomy) {
    $postTerm = term_exists( $termArray->slug, $taxonomy ); // array is returned if taxonomy is given
    if(empty($postTerm)) {
        $insertedTerm = wp_insert_term(
            $termArray->name,
            $taxonomy,
            array(
                'slug' => $termArray->slug
            )
        );
        $termID = $insertedTerm['term_id'];
        update_term_meta($termID, 'id_main_site', $termArray->term_id);
        update_term_meta($termID, 'parent_id_main_site', $termArray->parent_term_id);

    }
}

8. When imported, term is inserted in the database of the CHILD_SITE, the id of the inserted term is different than the PARENT_SITE term id . Therefore, PARENT_SITE term_id and parent_id of terms are saved in the CHILD_SITE of related taxonomy terms, so that it could be usedful in the future.

9. Run File to import the taxonomy terms, http://CHILD_SITE/app/books-category.php

Views: 166

Standard