1

I'm trying to find a way of disabling the selection of the parent category within Wordpress 3.5.1 (post editor screen) only when that parent category contains child categories.

My structure:

  • Category 1 (no children, allow users to post, keep selection option)
  • Galleries (parent category WITH children, remove selection option to stop users posting)
    • User 1 (child category, allow user to post, keep selection option)

A jQuery solution to disabling the section of all parent categories (regardless of having child categories or not) can be found here:

Make parent categories not selectable

5

I disabled the parent boxes to avoid shifting parents to the left.

add_action( 'admin_footer-post.php',     'wpse_98274_disable_top_categories_checkboxes' );
add_action( 'admin_footer-post-new.php', 'wpse_98274_disable_top_categories_checkboxes' );

/**
 * Disable parent checkboxes in Post Editor.
 */
function wpse_98274_disable_top_categories_checkboxes() {
    global $post_type;

    if ( 'post' != $post_type )
        return;
    ?>
        <script type="text/javascript">
            jQuery( "#category-all ul.children" ).each( function() {
                jQuery(this).closest( "ul" ).parent().children().children( "input" ).attr( 'disabled', 'disabled' )
            });
        </script>
    <?php
}

However, once the categories are pulled out of order for that horrid "feature" that moves them to the top of the box, the jQuery fails. I borrowed this code from a plugin.

add_filter( 'wp_terms_checklist_args', 'wpse_98274_checklist_args' );

/**
 * Remove horrid feature that places checked categories on top.
 */
function wpse_98274_checklist_args( $args ) {

    $args['checked_ontop'] = false;
    return $args;
}
0

If you're looking to use this with Custom Posts, replace where it says post in the if statement with the slug of your post type.

With Custom Taxonomies, replace where it says #category-all with the name of your taxonomy (e.g., #taxonomyname-all).

Your situation will most likely be both if you are looking at this.

For multiple taxonomies/post types at the same time: One simple option is to duplicate the entire code, and in addition to the changes above, make any new name where it says wpse_98274_disable_top_categories_checkboxes - the 98274 is just referencing this page (look at this pages URL) and not calling anything special in WordPress. Or more elegant is to make multiple if statements.

0

You can use this plugin, it will solve your issue

https://wordpress.org/plugins/adjust-admin-categories/

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.