Can I prevent the user from adding more than two levels deep of terms inside of a taxonomy metabox?

I’m in the process of creating a leveled tutorial structure in WordPress and want to build it so that it sets up the front-end with no code needed from the user.

So far, I have a custom post type called ‘tut_page’ that has a metabox for custom taxonomy called, ‘tut_group’ and terms can be added via the taxonomy metabox. For my register_taxonomy, the value for ‘hierarchical’ has been set to true. So I want levels, however I don’t want more than two deep. I know that I can choose how many levels of terms to display in a template, but is there a way where I can just prevent users from creating terms that are deeper than two levels from the taxonomy metabox?

Read More

Here’s what I have in functions.php for registering the taxonomy:

function create_tutorial_topic() {
    register_taxonomy(
        'tut_group', //name of taxonomy
        'tut_post', //object type e.g. our post type
        array(
                'labels' => array(
                'name' => _x( 'Tutorial Group', 'taxonomy general name' ),
                'singular_name' => _x( 'Tutorial Groups', 'taxonomy singular name' ),
                'search_items' =>  __( 'Search Tutorial Groups' ),
                'all_items' => __( 'All Tutorial Groups' ),
                'parent_item' => __( 'Parent Tutorial Groups' ),
                'parent_item_colon' => __( 'Parent Tutorial Groups:' ),
                'edit_item' => __( 'Edit Tutorial Group' ),
                'update_item' => __( 'Update Tutorial Group' ),
                'add_new_item' => __( 'Add Tutorial Group' ),
                'new_item_name' => __( 'New Tutorial Group' ),
                'menu_name' => __( 'Tutorial Group' ),
            ),
            'rewrite' => array( 'slug' => 'tutorial-group' ),
            'hierarchical' => true, //this is true but I only want two levels deep
        )
    );
}

I would also like to build as much this as much as I can myself without having to use a plugin. I tried searching on this site and found questions about limiting the number of terms, but not hierarchy levels. Thanks for any tips in advance!

Related posts

1 comment

  1. Unfortunately, this is not possible, at least not safe.

    The function wp_insert_term( $term, $taxonomy, $args = array() ) that handles the creation of taxonomies provides a few filters and hooks, but the only one before the creation of the term in the database is

    $term = apply_filters( 'pre_insert_term', $term, $taxonomy );
    

    As you can see, the $args provided in the creation of the term are not passed to the filter – so you have no way of checking how many levels of the taxonomy exists by looking up $args['parent'].

    An ugly way of doing it would be after the term is created, hooking into the action created_term or create_term, and checking it there. I have never done this myself, and you may open up a big can of worms deleting the term after creation with no possibility of returning an error afterwards.

    If the filter pre_insert_term would accept the $args as an argument, it would me no problem.

Comments are closed.