Programming, WordPress

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

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

 

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 a two meta field 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

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

Hits: 6

Standard