1

I would like to disable the "+Add Category Button" under the Category metabox so that anyone creating a post has to choose only from the list of existing categories.

  1. I did check out another post, which literally has the same question, How To Remove The "+ Add New Category" Link From A Category Metabox But the accepted answer doesn't make sense because he references Line 345-367, which are not relevant lines (maybe because of WP updates, things might have changed). So I am confused.

  2. I installed the 'User Role Editor' plugin and removed the 'manage_categories' capability for the 'Editor' which worked. But I would like to remove it for ALL users including admin, superadmin. So I tried changing the theme's functions.php as:

    add_action( 'add_meta_boxes', 'isa_remove_categories_meta_box' )
    function isa_remove_categories_meta_box() {
    
    remove_meta_box( 'categorydiv', 'post', 'side' );// remove the Categories box
    
    }
    

    But, this makes the entire Category box disappear. Is there anything I can use to replace the 'categorydiv' to make only the '+Add New Category' disappear?

Or, can someone point me out to what the accepted answer from the other post meant by lines 345-367? I can try that as well.

4
  • as the OP in the question you link to mentions, I use "CSS hacks to hide it" when I want this. Mar 10, 2017 at 17:05
  • Can you please expand on how to go about with CSS hacks? Which CSS files would I have to modify? Mar 10, 2017 at 17:11
  • the answer to that other question also mentions "Currently there is no hook available to filter the output". About 10 days ago I opened a Trac ticket and requested that such a filter be added (admittedly, for a different reason). It doesn't appear that the core team has even reviewed that ticket, but maybe if you (and others) commented on it saying it would be useful to you as well, they'll act on it. Mar 10, 2017 at 17:12
  • In your plugin/theme add the following #category-adder { display: none ;} to any CSS you enqueue on admin_enqueue_scripts Mar 10, 2017 at 17:21

3 Answers 3

1

I installed the 'User Role Editor' plugin and removed the 'manage_categories' capability for the 'Editor' which worked. But I would like to remove it for ALL users including admin, superadmin.

If removing the 'manage_categories' capability from the editor role provides the functionality you want, then you can remove the 'manage_categories' capability from all user roles fairly easily either by using an existing plugin, or writing a new plugin that upon activation removes the capability from all the user roles. Here's the code that would be necessary to remove the capability from each role (actually, it explicitly sets the capability to false).

register_activation_hook( __FILE__, 'wpse_259647_remove_manage_categories_cap' );
function wpse_259647_remove_manage_categories_cap() {
  $roles = wp_roles();
  foreach( $roles->role_names as $slug => $name ) {
    $role = get_role( $slug );
    $role->add_cap( 'manage_categories', false );
  }
}

This won't remove the capability from "super admins" though since "super admins" technically aren't a role or a capability.

If you want to explicitly revoke the manage_categories capability from all users, you could do that too.

register_activation_hook( __FILE__, 'wpse_259647_remove_manage_categories_cap' );
function wpse_259647_remove_manage_categories_cap() {
  $users = get_users();
  foreach( $users as $user ) {
    $user->add_cap( 'manage_categories', false );
  }
}

This may still not remove the capability from "super admins" though, because WordPress treats them differently than other users.

2

If you are building your own taxonomy you can set the capabilities when registering the taxonomy. For example...

<?php
    register_taxonomy(
        'members_tax',
        'post',
        array(
            'label'             => __( 'Members' ),
            'hierarchical'      => true,
            'rewrite'           => array( 'slug' => 'members-tax' ),
            'capabilities'      => array(
                'assign_terms' => 'manage_options',
                'edit_terms'   => 'god',
                'manage_terms' => 'god',
            ),
            'show_in_nav_menus' => false,
        )
    );

If you want only the administrator to be able to add or edit change god to administrator. It removes the "Add New Members Category" from the metabox on new posts page and removes the link on the menu bar under posts.

This comes from this gist https://gist.github.com/ChrisFlannagan/4cd3bfd0e853cda3d3f7898c59428ac2

1
  • Follow up. If you want any user to be assign terms remove the 'assign_terms' => 'manage_options',
    – CDToad
    Aug 7, 2019 at 14:26
0

The Category's metabox is set when the category is registered with register_taxonomy function. The default behavior of this function is to set the metabox callback (meta_box_cb) to a wordpress function called post_categories_meta_box. It is this function that renders the "+Add Category" Button. Unfortunately, this metabox does not allow you to selectively hide this button. As other answerers pointed out, it will hide that button based on the taxonomy's edit_terms capability, so you can effectively hide it by limiting who has this capability. However, this does not work if, like me, you want to hide this button without limiting capabilities.

To do this you need to supply your own meta_box_cb. I did this by simply copying post_categories_meta_box and deleting that button.

My custom function:

function custom_post_categories_meta_box_without_add_new( $post, $box ) {
    $defaults = array( 'taxonomy' => 'category' );
    if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
        $args = array();
    } else {
        $args = $box['args'];
    }
    $parsed_args = wp_parse_args( $args, $defaults );
    $tax_name    = esc_attr( $parsed_args['taxonomy'] );
    $taxonomy    = get_taxonomy( $parsed_args['taxonomy'] );
    ?>
    <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">
        <ul id="<?php echo $tax_name; ?>-tabs" class="category-tabs">
            <li class="tabs"><a href="#<?php echo $tax_name; ?>-all"><?php echo $taxonomy->labels->all_items; ?></a></li>
            <li class="hide-if-no-js"><a href="#<?php echo $tax_name; ?>-pop"><?php echo esc_html( $taxonomy->labels->most_used ); ?></a></li>
        </ul>
 
        <div id="<?php echo $tax_name; ?>-pop" class="tabs-panel" style="display: none;">
            <ul id="<?php echo $tax_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
                <?php $popular_ids = wp_popular_terms_checklist( $tax_name ); ?>
            </ul>
        </div>
 
        <div id="<?php echo $tax_name; ?>-all" class="tabs-panel">
            <?php
            $name = ( 'category' === $tax_name ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
            // Allows for an empty term set to be sent. 0 is an invalid term ID and will be ignored by empty() checks.
            echo "<input type='hidden' name='{$name}[]' value='0' />";
            ?>
            <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
                <?php
                wp_terms_checklist(
                    $post->ID,
                    array(
                        'taxonomy'     => $tax_name,
                        'popular_cats' => $popular_ids,
                    )
                );
                ?>
            </ul>
        </div>
    </div>
    <?php
}

(I hope I can include here my custom function which as I mentioned is largely copied from wordpress. If there are legal reasons I cannot do this, please let me know.)

Applied to my custom taxonomy

register_taxonomy(
    "my_custom_taxonomy",
    "my_custom_type",
    array(
        ...
        'hierarchical'      => true,
        'meta_box_cb' => 'custom_post_categories_meta_box_without_add_new'
    )
);

The downside of this is that, unlike the default, your meta_box_cb will not change if wordpress updates their styling.

Note that this only works if you are registering your taxonomies manually. If you are using a plugin, it may not allow you to configure the meta_box_cb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.