How to create 450 categories in wp












1














I am having trouble to find a way for how to add categories for all the products.



I have Apple, Samsung, Sony, Xiaomi, Huawei categories and each of there needs subcategories which will stand for models, and also specific part.



example of category tree:

Apple>Iphone 6s>Iphone 6s battery

Apple>Iphone 6s>Iphone 6s replacement screen>

Apple>Iphone 8>Iphone 8 battery

etc



All the products needs to be in separated category so I can show correct item under correct menu item and no other Iphone stuff, only the battery for example.



How do I create 500+ categories which will represent all the items we have got?



I was thinking to export existing database and edit in text editor and write all the categories there as it would be simpler then using category manager inside WordPress cpanel which is very bad for this, as I am getting lost of what is in there and what isn't.



Do you think it will work this way? Or what do you suggest?
Thanks!










share|improve this question







New contributor




Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1














    I am having trouble to find a way for how to add categories for all the products.



    I have Apple, Samsung, Sony, Xiaomi, Huawei categories and each of there needs subcategories which will stand for models, and also specific part.



    example of category tree:

    Apple>Iphone 6s>Iphone 6s battery

    Apple>Iphone 6s>Iphone 6s replacement screen>

    Apple>Iphone 8>Iphone 8 battery

    etc



    All the products needs to be in separated category so I can show correct item under correct menu item and no other Iphone stuff, only the battery for example.



    How do I create 500+ categories which will represent all the items we have got?



    I was thinking to export existing database and edit in text editor and write all the categories there as it would be simpler then using category manager inside WordPress cpanel which is very bad for this, as I am getting lost of what is in there and what isn't.



    Do you think it will work this way? Or what do you suggest?
    Thanks!










    share|improve this question







    New contributor




    Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1







      I am having trouble to find a way for how to add categories for all the products.



      I have Apple, Samsung, Sony, Xiaomi, Huawei categories and each of there needs subcategories which will stand for models, and also specific part.



      example of category tree:

      Apple>Iphone 6s>Iphone 6s battery

      Apple>Iphone 6s>Iphone 6s replacement screen>

      Apple>Iphone 8>Iphone 8 battery

      etc



      All the products needs to be in separated category so I can show correct item under correct menu item and no other Iphone stuff, only the battery for example.



      How do I create 500+ categories which will represent all the items we have got?



      I was thinking to export existing database and edit in text editor and write all the categories there as it would be simpler then using category manager inside WordPress cpanel which is very bad for this, as I am getting lost of what is in there and what isn't.



      Do you think it will work this way? Or what do you suggest?
      Thanks!










      share|improve this question







      New contributor




      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am having trouble to find a way for how to add categories for all the products.



      I have Apple, Samsung, Sony, Xiaomi, Huawei categories and each of there needs subcategories which will stand for models, and also specific part.



      example of category tree:

      Apple>Iphone 6s>Iphone 6s battery

      Apple>Iphone 6s>Iphone 6s replacement screen>

      Apple>Iphone 8>Iphone 8 battery

      etc



      All the products needs to be in separated category so I can show correct item under correct menu item and no other Iphone stuff, only the battery for example.



      How do I create 500+ categories which will represent all the items we have got?



      I was thinking to export existing database and edit in text editor and write all the categories there as it would be simpler then using category manager inside WordPress cpanel which is very bad for this, as I am getting lost of what is in there and what isn't.



      Do you think it will work this way? Or what do you suggest?
      Thanks!







      categories mysql






      share|improve this question







      New contributor




      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 18 hours ago









      Martin Valihora

      82




      82




      New contributor




      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Martin Valihora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can try using BulkPress. It allows you to create hundreds of categories very easily. Follow these steps:




          1. Install and activate the plugin.

          2. In the left side bar, hover your cursor on Bulkpress. Then click
            on Terms.

          3. A new page will appear. In Taxonomy field, choose Category.

          4. Insert your Categories in the Terms field.

          5. Choose your desired Parent, if required.

          6. Finally, click on the Add Terms button.






          share|improve this answer































            2














            Wouldn't it be handy if we could write our category hierarchy that we wish to import as:



            apple|Apple|Apple products
            iphone-6s|Iphone 6s|Apple Iphone 6s
            iphone-6s-battery|Iphone 6s battery|Apple Iphone 6s battery
            iphone-6s-replacement-screen|Iphone 6s replacement screen|Iphone 6s replacement screen
            iphone-8-battery|Iphone 8 battery|Apple Iphone 8 battery


            with each row as:



            term slug|term name|term description


            and the hierarchy defined by the indentation.



            Another example:



            a1|A1|Description
            a11|A11|Description
            a111|A111|Description
            a112|A112|Description
            a12|A12|Description
            a121|A121|Description
            a122|A122|Description
            a13|A13|Description
            a2|A2|Description


            One usage suggestion could be:



            $import = '
            a1|A1|Description
            a11|A11|Description
            a111|A111|Description
            a112|A112|Description
            a12|A12|Description
            ';

            wpse324129_bulk_term_importer( $import, 'category', '|' );


            that would import the terms as:



            import



            Here's a first draft for such a function to bulk import terms from a string:



            /**
            * Bulk import terms with a given hierarchy.
            *
            * @param string $import Terms to import
            * @param string $tax Taxonomy. Default 'category'.
            * @param string $delimiter Delimiter. Default '|'.
            */
            function wpse324129_bulk_term_importer( $import, $tax = 'category', $delimiter = '|' ) {
            $rows = explode( PHP_EOL, trim( $import ) );
            $level = 0;
            $prev_term_id = 0;
            $prev_level = 0;
            $direction = 0;
            $ancestors = ;

            foreach( $rows as $row ) {
            $cols = str_getcsv( $row, $delimiter );

            if ( 3 !== count( $cols ) ) {
            throw new Exception( __( 'Incorrect number of columns', 'wpse' ) );
            }

            $term_slug = $cols[0];
            $term_name = $cols[1];
            $term_desc = $cols[2];

            // Helper parameters for the hierarchy traversal.
            $prev_level = $level;
            $level = strlen( $term_slug ) - strlen( ltrim( $term_slug ) );
            $direction = $level - $prev_level; // Traversing direction:
            // right(+), left(-), none(0).

            // Collect the info on direct term ancestors, when we go right.
            if ( $direction > 0 && $prev_term_id > 0 ) {
            array_push( $ancestors, $prev_term_id );
            }

            // Reduce ancestors when we go left (multiple array pop).
            if ( $direction < 0 ) {
            $ancestors = array_slice( $ancestors, 0, count( $ancestors ) + $direction );
            }

            // Arguments for term creation.
            $args = [
            'description' => $term_desc,
            'slug' => $term_slug,
            ];

            // Check parent term and add to the term creation arguments if needed.
            if ( $prev_term_id > 0 && count( $ancestors ) > 0 ) {
            $parent_id = end( $ancestors );
            $parent_term = get_term_by( 'term_id', $parent_id, $tax, ARRAY_A );
            if ( isset( $parent_term['term_id'] ) ) {
            $args['parent'] = $parent_term['term_id'];
            }
            }

            // Check if current term slug exists and insert if needed.
            $term = get_term_by( 'slug', $term_slug, $tax, ARRAY_A );
            if ( ! isset( $term['term_id'] ) ) {
            $result = wp_insert_term( $term_slug, $tax, $args );
            if ( is_wp_error( $result ) ) {
            throw new Exception( __( 'Could not insert term!', 'wpse' ) );
            }
            $term_id = $result['term_id'];
            } else {
            $term_id = $term['term_id'];
            }

            $prev_term_id = $term_id;
            }
            }


            For large import, one could run it through the wp-cli to avoid timeout.



            Hope you can extend this further to your needs, e.g. with a format validator.



            Please backup before testing!






            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "110"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              Martin Valihora is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f324129%2fhow-to-create-450-categories-in-wp%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can try using BulkPress. It allows you to create hundreds of categories very easily. Follow these steps:




              1. Install and activate the plugin.

              2. In the left side bar, hover your cursor on Bulkpress. Then click
                on Terms.

              3. A new page will appear. In Taxonomy field, choose Category.

              4. Insert your Categories in the Terms field.

              5. Choose your desired Parent, if required.

              6. Finally, click on the Add Terms button.






              share|improve this answer




























                0














                You can try using BulkPress. It allows you to create hundreds of categories very easily. Follow these steps:




                1. Install and activate the plugin.

                2. In the left side bar, hover your cursor on Bulkpress. Then click
                  on Terms.

                3. A new page will appear. In Taxonomy field, choose Category.

                4. Insert your Categories in the Terms field.

                5. Choose your desired Parent, if required.

                6. Finally, click on the Add Terms button.






                share|improve this answer


























                  0












                  0








                  0






                  You can try using BulkPress. It allows you to create hundreds of categories very easily. Follow these steps:




                  1. Install and activate the plugin.

                  2. In the left side bar, hover your cursor on Bulkpress. Then click
                    on Terms.

                  3. A new page will appear. In Taxonomy field, choose Category.

                  4. Insert your Categories in the Terms field.

                  5. Choose your desired Parent, if required.

                  6. Finally, click on the Add Terms button.






                  share|improve this answer














                  You can try using BulkPress. It allows you to create hundreds of categories very easily. Follow these steps:




                  1. Install and activate the plugin.

                  2. In the left side bar, hover your cursor on Bulkpress. Then click
                    on Terms.

                  3. A new page will appear. In Taxonomy field, choose Category.

                  4. Insert your Categories in the Terms field.

                  5. Choose your desired Parent, if required.

                  6. Finally, click on the Add Terms button.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 hours ago

























                  answered 17 hours ago









                  Md. Ehsanul Haque Kanan

                  805




                  805

























                      2














                      Wouldn't it be handy if we could write our category hierarchy that we wish to import as:



                      apple|Apple|Apple products
                      iphone-6s|Iphone 6s|Apple Iphone 6s
                      iphone-6s-battery|Iphone 6s battery|Apple Iphone 6s battery
                      iphone-6s-replacement-screen|Iphone 6s replacement screen|Iphone 6s replacement screen
                      iphone-8-battery|Iphone 8 battery|Apple Iphone 8 battery


                      with each row as:



                      term slug|term name|term description


                      and the hierarchy defined by the indentation.



                      Another example:



                      a1|A1|Description
                      a11|A11|Description
                      a111|A111|Description
                      a112|A112|Description
                      a12|A12|Description
                      a121|A121|Description
                      a122|A122|Description
                      a13|A13|Description
                      a2|A2|Description


                      One usage suggestion could be:



                      $import = '
                      a1|A1|Description
                      a11|A11|Description
                      a111|A111|Description
                      a112|A112|Description
                      a12|A12|Description
                      ';

                      wpse324129_bulk_term_importer( $import, 'category', '|' );


                      that would import the terms as:



                      import



                      Here's a first draft for such a function to bulk import terms from a string:



                      /**
                      * Bulk import terms with a given hierarchy.
                      *
                      * @param string $import Terms to import
                      * @param string $tax Taxonomy. Default 'category'.
                      * @param string $delimiter Delimiter. Default '|'.
                      */
                      function wpse324129_bulk_term_importer( $import, $tax = 'category', $delimiter = '|' ) {
                      $rows = explode( PHP_EOL, trim( $import ) );
                      $level = 0;
                      $prev_term_id = 0;
                      $prev_level = 0;
                      $direction = 0;
                      $ancestors = ;

                      foreach( $rows as $row ) {
                      $cols = str_getcsv( $row, $delimiter );

                      if ( 3 !== count( $cols ) ) {
                      throw new Exception( __( 'Incorrect number of columns', 'wpse' ) );
                      }

                      $term_slug = $cols[0];
                      $term_name = $cols[1];
                      $term_desc = $cols[2];

                      // Helper parameters for the hierarchy traversal.
                      $prev_level = $level;
                      $level = strlen( $term_slug ) - strlen( ltrim( $term_slug ) );
                      $direction = $level - $prev_level; // Traversing direction:
                      // right(+), left(-), none(0).

                      // Collect the info on direct term ancestors, when we go right.
                      if ( $direction > 0 && $prev_term_id > 0 ) {
                      array_push( $ancestors, $prev_term_id );
                      }

                      // Reduce ancestors when we go left (multiple array pop).
                      if ( $direction < 0 ) {
                      $ancestors = array_slice( $ancestors, 0, count( $ancestors ) + $direction );
                      }

                      // Arguments for term creation.
                      $args = [
                      'description' => $term_desc,
                      'slug' => $term_slug,
                      ];

                      // Check parent term and add to the term creation arguments if needed.
                      if ( $prev_term_id > 0 && count( $ancestors ) > 0 ) {
                      $parent_id = end( $ancestors );
                      $parent_term = get_term_by( 'term_id', $parent_id, $tax, ARRAY_A );
                      if ( isset( $parent_term['term_id'] ) ) {
                      $args['parent'] = $parent_term['term_id'];
                      }
                      }

                      // Check if current term slug exists and insert if needed.
                      $term = get_term_by( 'slug', $term_slug, $tax, ARRAY_A );
                      if ( ! isset( $term['term_id'] ) ) {
                      $result = wp_insert_term( $term_slug, $tax, $args );
                      if ( is_wp_error( $result ) ) {
                      throw new Exception( __( 'Could not insert term!', 'wpse' ) );
                      }
                      $term_id = $result['term_id'];
                      } else {
                      $term_id = $term['term_id'];
                      }

                      $prev_term_id = $term_id;
                      }
                      }


                      For large import, one could run it through the wp-cli to avoid timeout.



                      Hope you can extend this further to your needs, e.g. with a format validator.



                      Please backup before testing!






                      share|improve this answer




























                        2














                        Wouldn't it be handy if we could write our category hierarchy that we wish to import as:



                        apple|Apple|Apple products
                        iphone-6s|Iphone 6s|Apple Iphone 6s
                        iphone-6s-battery|Iphone 6s battery|Apple Iphone 6s battery
                        iphone-6s-replacement-screen|Iphone 6s replacement screen|Iphone 6s replacement screen
                        iphone-8-battery|Iphone 8 battery|Apple Iphone 8 battery


                        with each row as:



                        term slug|term name|term description


                        and the hierarchy defined by the indentation.



                        Another example:



                        a1|A1|Description
                        a11|A11|Description
                        a111|A111|Description
                        a112|A112|Description
                        a12|A12|Description
                        a121|A121|Description
                        a122|A122|Description
                        a13|A13|Description
                        a2|A2|Description


                        One usage suggestion could be:



                        $import = '
                        a1|A1|Description
                        a11|A11|Description
                        a111|A111|Description
                        a112|A112|Description
                        a12|A12|Description
                        ';

                        wpse324129_bulk_term_importer( $import, 'category', '|' );


                        that would import the terms as:



                        import



                        Here's a first draft for such a function to bulk import terms from a string:



                        /**
                        * Bulk import terms with a given hierarchy.
                        *
                        * @param string $import Terms to import
                        * @param string $tax Taxonomy. Default 'category'.
                        * @param string $delimiter Delimiter. Default '|'.
                        */
                        function wpse324129_bulk_term_importer( $import, $tax = 'category', $delimiter = '|' ) {
                        $rows = explode( PHP_EOL, trim( $import ) );
                        $level = 0;
                        $prev_term_id = 0;
                        $prev_level = 0;
                        $direction = 0;
                        $ancestors = ;

                        foreach( $rows as $row ) {
                        $cols = str_getcsv( $row, $delimiter );

                        if ( 3 !== count( $cols ) ) {
                        throw new Exception( __( 'Incorrect number of columns', 'wpse' ) );
                        }

                        $term_slug = $cols[0];
                        $term_name = $cols[1];
                        $term_desc = $cols[2];

                        // Helper parameters for the hierarchy traversal.
                        $prev_level = $level;
                        $level = strlen( $term_slug ) - strlen( ltrim( $term_slug ) );
                        $direction = $level - $prev_level; // Traversing direction:
                        // right(+), left(-), none(0).

                        // Collect the info on direct term ancestors, when we go right.
                        if ( $direction > 0 && $prev_term_id > 0 ) {
                        array_push( $ancestors, $prev_term_id );
                        }

                        // Reduce ancestors when we go left (multiple array pop).
                        if ( $direction < 0 ) {
                        $ancestors = array_slice( $ancestors, 0, count( $ancestors ) + $direction );
                        }

                        // Arguments for term creation.
                        $args = [
                        'description' => $term_desc,
                        'slug' => $term_slug,
                        ];

                        // Check parent term and add to the term creation arguments if needed.
                        if ( $prev_term_id > 0 && count( $ancestors ) > 0 ) {
                        $parent_id = end( $ancestors );
                        $parent_term = get_term_by( 'term_id', $parent_id, $tax, ARRAY_A );
                        if ( isset( $parent_term['term_id'] ) ) {
                        $args['parent'] = $parent_term['term_id'];
                        }
                        }

                        // Check if current term slug exists and insert if needed.
                        $term = get_term_by( 'slug', $term_slug, $tax, ARRAY_A );
                        if ( ! isset( $term['term_id'] ) ) {
                        $result = wp_insert_term( $term_slug, $tax, $args );
                        if ( is_wp_error( $result ) ) {
                        throw new Exception( __( 'Could not insert term!', 'wpse' ) );
                        }
                        $term_id = $result['term_id'];
                        } else {
                        $term_id = $term['term_id'];
                        }

                        $prev_term_id = $term_id;
                        }
                        }


                        For large import, one could run it through the wp-cli to avoid timeout.



                        Hope you can extend this further to your needs, e.g. with a format validator.



                        Please backup before testing!






                        share|improve this answer


























                          2












                          2








                          2






                          Wouldn't it be handy if we could write our category hierarchy that we wish to import as:



                          apple|Apple|Apple products
                          iphone-6s|Iphone 6s|Apple Iphone 6s
                          iphone-6s-battery|Iphone 6s battery|Apple Iphone 6s battery
                          iphone-6s-replacement-screen|Iphone 6s replacement screen|Iphone 6s replacement screen
                          iphone-8-battery|Iphone 8 battery|Apple Iphone 8 battery


                          with each row as:



                          term slug|term name|term description


                          and the hierarchy defined by the indentation.



                          Another example:



                          a1|A1|Description
                          a11|A11|Description
                          a111|A111|Description
                          a112|A112|Description
                          a12|A12|Description
                          a121|A121|Description
                          a122|A122|Description
                          a13|A13|Description
                          a2|A2|Description


                          One usage suggestion could be:



                          $import = '
                          a1|A1|Description
                          a11|A11|Description
                          a111|A111|Description
                          a112|A112|Description
                          a12|A12|Description
                          ';

                          wpse324129_bulk_term_importer( $import, 'category', '|' );


                          that would import the terms as:



                          import



                          Here's a first draft for such a function to bulk import terms from a string:



                          /**
                          * Bulk import terms with a given hierarchy.
                          *
                          * @param string $import Terms to import
                          * @param string $tax Taxonomy. Default 'category'.
                          * @param string $delimiter Delimiter. Default '|'.
                          */
                          function wpse324129_bulk_term_importer( $import, $tax = 'category', $delimiter = '|' ) {
                          $rows = explode( PHP_EOL, trim( $import ) );
                          $level = 0;
                          $prev_term_id = 0;
                          $prev_level = 0;
                          $direction = 0;
                          $ancestors = ;

                          foreach( $rows as $row ) {
                          $cols = str_getcsv( $row, $delimiter );

                          if ( 3 !== count( $cols ) ) {
                          throw new Exception( __( 'Incorrect number of columns', 'wpse' ) );
                          }

                          $term_slug = $cols[0];
                          $term_name = $cols[1];
                          $term_desc = $cols[2];

                          // Helper parameters for the hierarchy traversal.
                          $prev_level = $level;
                          $level = strlen( $term_slug ) - strlen( ltrim( $term_slug ) );
                          $direction = $level - $prev_level; // Traversing direction:
                          // right(+), left(-), none(0).

                          // Collect the info on direct term ancestors, when we go right.
                          if ( $direction > 0 && $prev_term_id > 0 ) {
                          array_push( $ancestors, $prev_term_id );
                          }

                          // Reduce ancestors when we go left (multiple array pop).
                          if ( $direction < 0 ) {
                          $ancestors = array_slice( $ancestors, 0, count( $ancestors ) + $direction );
                          }

                          // Arguments for term creation.
                          $args = [
                          'description' => $term_desc,
                          'slug' => $term_slug,
                          ];

                          // Check parent term and add to the term creation arguments if needed.
                          if ( $prev_term_id > 0 && count( $ancestors ) > 0 ) {
                          $parent_id = end( $ancestors );
                          $parent_term = get_term_by( 'term_id', $parent_id, $tax, ARRAY_A );
                          if ( isset( $parent_term['term_id'] ) ) {
                          $args['parent'] = $parent_term['term_id'];
                          }
                          }

                          // Check if current term slug exists and insert if needed.
                          $term = get_term_by( 'slug', $term_slug, $tax, ARRAY_A );
                          if ( ! isset( $term['term_id'] ) ) {
                          $result = wp_insert_term( $term_slug, $tax, $args );
                          if ( is_wp_error( $result ) ) {
                          throw new Exception( __( 'Could not insert term!', 'wpse' ) );
                          }
                          $term_id = $result['term_id'];
                          } else {
                          $term_id = $term['term_id'];
                          }

                          $prev_term_id = $term_id;
                          }
                          }


                          For large import, one could run it through the wp-cli to avoid timeout.



                          Hope you can extend this further to your needs, e.g. with a format validator.



                          Please backup before testing!






                          share|improve this answer














                          Wouldn't it be handy if we could write our category hierarchy that we wish to import as:



                          apple|Apple|Apple products
                          iphone-6s|Iphone 6s|Apple Iphone 6s
                          iphone-6s-battery|Iphone 6s battery|Apple Iphone 6s battery
                          iphone-6s-replacement-screen|Iphone 6s replacement screen|Iphone 6s replacement screen
                          iphone-8-battery|Iphone 8 battery|Apple Iphone 8 battery


                          with each row as:



                          term slug|term name|term description


                          and the hierarchy defined by the indentation.



                          Another example:



                          a1|A1|Description
                          a11|A11|Description
                          a111|A111|Description
                          a112|A112|Description
                          a12|A12|Description
                          a121|A121|Description
                          a122|A122|Description
                          a13|A13|Description
                          a2|A2|Description


                          One usage suggestion could be:



                          $import = '
                          a1|A1|Description
                          a11|A11|Description
                          a111|A111|Description
                          a112|A112|Description
                          a12|A12|Description
                          ';

                          wpse324129_bulk_term_importer( $import, 'category', '|' );


                          that would import the terms as:



                          import



                          Here's a first draft for such a function to bulk import terms from a string:



                          /**
                          * Bulk import terms with a given hierarchy.
                          *
                          * @param string $import Terms to import
                          * @param string $tax Taxonomy. Default 'category'.
                          * @param string $delimiter Delimiter. Default '|'.
                          */
                          function wpse324129_bulk_term_importer( $import, $tax = 'category', $delimiter = '|' ) {
                          $rows = explode( PHP_EOL, trim( $import ) );
                          $level = 0;
                          $prev_term_id = 0;
                          $prev_level = 0;
                          $direction = 0;
                          $ancestors = ;

                          foreach( $rows as $row ) {
                          $cols = str_getcsv( $row, $delimiter );

                          if ( 3 !== count( $cols ) ) {
                          throw new Exception( __( 'Incorrect number of columns', 'wpse' ) );
                          }

                          $term_slug = $cols[0];
                          $term_name = $cols[1];
                          $term_desc = $cols[2];

                          // Helper parameters for the hierarchy traversal.
                          $prev_level = $level;
                          $level = strlen( $term_slug ) - strlen( ltrim( $term_slug ) );
                          $direction = $level - $prev_level; // Traversing direction:
                          // right(+), left(-), none(0).

                          // Collect the info on direct term ancestors, when we go right.
                          if ( $direction > 0 && $prev_term_id > 0 ) {
                          array_push( $ancestors, $prev_term_id );
                          }

                          // Reduce ancestors when we go left (multiple array pop).
                          if ( $direction < 0 ) {
                          $ancestors = array_slice( $ancestors, 0, count( $ancestors ) + $direction );
                          }

                          // Arguments for term creation.
                          $args = [
                          'description' => $term_desc,
                          'slug' => $term_slug,
                          ];

                          // Check parent term and add to the term creation arguments if needed.
                          if ( $prev_term_id > 0 && count( $ancestors ) > 0 ) {
                          $parent_id = end( $ancestors );
                          $parent_term = get_term_by( 'term_id', $parent_id, $tax, ARRAY_A );
                          if ( isset( $parent_term['term_id'] ) ) {
                          $args['parent'] = $parent_term['term_id'];
                          }
                          }

                          // Check if current term slug exists and insert if needed.
                          $term = get_term_by( 'slug', $term_slug, $tax, ARRAY_A );
                          if ( ! isset( $term['term_id'] ) ) {
                          $result = wp_insert_term( $term_slug, $tax, $args );
                          if ( is_wp_error( $result ) ) {
                          throw new Exception( __( 'Could not insert term!', 'wpse' ) );
                          }
                          $term_id = $result['term_id'];
                          } else {
                          $term_id = $term['term_id'];
                          }

                          $prev_term_id = $term_id;
                          }
                          }


                          For large import, one could run it through the wp-cli to avoid timeout.



                          Hope you can extend this further to your needs, e.g. with a format validator.



                          Please backup before testing!







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 4 hours ago

























                          answered 13 hours ago









                          birgire

                          52.5k459137




                          52.5k459137






















                              Martin Valihora is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              Martin Valihora is a new contributor. Be nice, and check out our Code of Conduct.













                              Martin Valihora is a new contributor. Be nice, and check out our Code of Conduct.












                              Martin Valihora is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to WordPress Development Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f324129%2fhow-to-create-450-categories-in-wp%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Morgemoulin

                              Scott Moir

                              Souastre