How To Quickly Add Child Categories In WordPress

Adding child category and blog’s hierachy is as tedious task if you have many categories to manage. My website has a lot of categories too so I think there must be a better way to add child categories to current category.

So, I decided to make a plugin to solve this problem. It is a very simple plugin that help you quickly create child categories. Initially, it supports WordPress’ categories only. Now, WooCommerce categories are also supported.

Since the code is very simple, you can implement the functions without installing a plugin. I’ll provide the code in the section 2 of this post.

Quickly adding child categories using plugin

You can download the plugin below:

 

As you can see, when you click on the + child button, the Parent category select box is automatically changed to the category you select. Then, you can simply go ahead and details for the child category and click on add new category button as usual.

I’m submitting the plugin to WordPress.org so you can download the plugin directly from that site. I also added support for WooCommerce categories so you can now quickly add child categories to current WooCommerce categories too.

Quickly adding child categories WITHOUT using plugin

If you don’t want to use the plugin, you can paste the code below in the functions.php file of your child theme. Click here if you don’t have one and wonder how to create a child theme in WordPress.

 

add_filter('category_row_actions', 'binary_carpenter_quick_add_child_add_option', 10, 2);
add_filter('product_cat_row_actions', 'binary_carpenter_quick_add_child_add_option', 10, 2);
add_action('admin_print_scripts', 'binary_carpenter_quick_add_child_print_js', 10, 2);

function binary_carpenter_quick_add_child_print_js()
{

    global $current_screen;

        if ($current_screen->id === 'edit-category' || $current_screen->id === 'edit-product_cat')
        { ?>
            <script>

                document.addEventListener("DOMContentLoaded", function(event) {

                    var add_child_buttons = document.getElementsByClassName('bcqac-item');

                    for (var i = 0; i < add_child_buttons.length; i++)
                    {
                        add_child_buttons[i].addEventListener('click', function(e){
                            e.preventDefault();
                            //set the parent category to the current parent category
                            var parent_id = this.getAttribute('data-parent-id');
                            var parent_select = document.getElementById("parent");
                            parent_select.value = parent_id;

                        });
                    }

                });



            </script>
        <?php }
}

function binary_carpenter_quick_add_child_add_option($actions, $tag)
{
  $actions['add_child'] = sprintf(
    '<a href="#" data-parent-id="%s" class="bcqac-item" >%s</a>',
    $tag->term_id,
    /* translators: %s: taxonomy term name */
    __( '+ child' )
  );

  return $actions;
}

Save the functions.php file and you are done. Now, you can quickly add child categories in WordPress and WooCommerce.

Conclusion

I can now quickly create child category in WordPress and WooCommerce. As I’m creating the hierarchy for this blog, this tool becomes very handy. If you have any suggestions, please let me know in the comment section below.

Click on a star to rate it!

Average rating / 5. Vote count:

Leave a Reply

Your email address will not be published. Required fields are marked *