WordPress: Remove “Parent” dropdown from category form

Hi I’ve downloaded a plugin “Simple Staff List” and it does what I need but I don’t want editors to create a sub-category. How can I remove/hide the “Parent” selectbox on the form?parent selectobox on form

Related posts

Leave a Reply

6 comments

  1. Add bellow code in your current theme function.php file.

    add_action( 'admin_head-edit-tags.php', 'wpse_58799_remove_parent_category' );
    
    function wpse_58799_remove_parent_category()
    {
        if ( 'category' != $_GET['taxonomy'] )
            return;
    
        $parent = 'parent()';
    
        if ( isset( $_GET['action'] ) )
            $parent = 'parent().parent()';
    
        ?>
            <script type="text/javascript">
                jQuery(document).ready(function($)
                {     
                    $('label[for=parent]').<?php echo $parent; ?>.remove();       
                });
            </script>
        <?php
    }
    
  2. You can use set these options in
    register_taxonomy() func

    'hierarchical' => false,
    'parent_item'  => null,
    'parent_item_colon' => null,
    

    This would remove the parent field.

  3. This will remove the parent dropdown from both the taxonomy and post new/edit screens.

    <?php
    
    function remove_tax_parent_dropdown() {
        $screen = get_current_screen();
    
        if ( 'category' == $screen->taxonomy ) {
            if ( 'edit-tags' == $screen->base ) {
                $parent = "$('label[for=parent]').parent()";
            } elseif ( 'term' == $screen->base ) {
                $parent = "$('label[for=parent]').parent().parent()";
            }
        } elseif ( 'post' == $screen->post_type ) {
            $parent = "$('#newcategory_parent')";
        } else {
            return;
        }
        ?>
    
        <script type="text/javascript">
            jQuery(document).ready(function($) {     
                <?php echo $parent; ?>.remove();       
            });
        </script>
    
        <?php 
    }
    add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' ); 
    
  4. If you want to disable “hierarchical” itself from category taxonomy, add this code in your function.php.

    add_action('init', function(){
        global $wp_taxonomies;
        $wp_taxonomies['category']->hierarchical = false;
    });
    
  5. Add bellow code in your current theme function.php file.

    function custom_taxonomy() {
    
        $labels = array(
            'name'                       => _x( 'Brands', 'Taxonomy General Name', 'text_domain' ),
            'singular_name'              => _x( 'Brand', 'Taxonomy Singular Name', 'text_domain' ),
            'menu_name'                  => __( 'Taxonomy', 'text_domain' ),
            'all_items'                  => __( 'All Items', 'text_domain' ),
            'parent_item'                => __( 'Parent Item', 'text_domain' ),
            'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
            'new_item_name'              => __( 'New Item Name', 'text_domain' ),
            'add_new_item'               => __( 'Add New Item', 'text_domain' ),
            'edit_item'                  => __( 'Edit Item', 'text_domain' ),
            'update_item'                => __( 'Update Item', 'text_domain' ),
            'view_item'                  => __( 'View Item', 'text_domain' ),
            'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
            'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
            'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
            'popular_items'              => __( 'Popular Items', 'text_domain' ),
            'search_items'               => __( 'Search Items', 'text_domain' ),
            'not_found'                  => __( 'Not Found', 'text_domain' ),
            'no_terms'                   => __( 'No items', 'text_domain' ),
            'items_list'                 => __( 'Items list', 'text_domain' ),
            'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
        );
        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'parent_item'                => null,
            'parent_item_colon'          => null,
        );
        register_taxonomy( 'brands', array( 'product' ), $args );
    
    }
    add_action( 'init', 'custom_taxonomy', 0 );
    

    enter image description here

    Reference: https://codex.wordpress.org/Function_Reference/register_taxonomy

  6. This will work with WordPress 5.4.2. For me all other solutions show the fields as long as jQuery removes them. My quick and dirty solution hides via CSS and remove them with jQuery. Unfortunately only hiding (not removing) seems to work with Gutenberg Editor. Maybe someone else has another solution.

    function remove_tax_parent_dropdown() {
        $screen = get_current_screen();
        
        if ( 'category' == $screen->taxonomy ) {
            if ( 'edit-tags' == $screen->base ) {
                $parent = "$('label[for=parent]').parent().remove(); ";
                $css = ".term-parent-wrap{display:none;}";
            } elseif ( 'term' == $screen->base ) {
                $parent = "$('label[for=parent]').parent().parent().remove(); ";
                $css = ".term-parent-wrap{display:none;}";
            }
        } elseif ( 'post' == $screen->post_type ) {
            $parent = "$('#newcategory_parent').remove();";
            $css = "div.components-base-control:nth-child(3){display:none;}";
        } else {
            return;
        }
        
        if(!empty($css)) {
            echo '<style type="text/css">';
                echo $css;
            echo '</style>';
        }
        
        if(!empty($parent)) {
            echo '<script type="text/javascript">';
                echo 'jQuery(document).ready(function($) {';     
                echo $parent;      
                echo '});';
            echo '</script>';
        }
    }
    add_action( 'admin_head-edit-tags.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-term.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-post.php', 'remove_tax_parent_dropdown' );
    add_action( 'admin_head-post-new.php', 'remove_tax_parent_dropdown' ); 
    

    Ah by the way – DON’T use the following code because you get bad issues with it. When you are saving a post without changing category, all categories of this post will be deleted and the category IDs will be created as new categories and added to your post.

    global $wp_taxonomies;
    $wp_taxonomies['category']->hierarchical = false;