• AWKampen

    (@awkampen)


    I’m trying to make a community website in which users can create their own categories (for pages). I want to restrict their options.

    I have six parent categories and I want the category-creation abilities to be limited to being a child-category of one of those six categories. Besides that, I don’t want the user to be able te create any sub-categories that are NOT a child of one of my six parent-categories.

    Just to be clear: The user can see every category there is. It’s the creation of a new category I want to restrict to 1 level only!

    I’ve been trying several things, but I can’t seem to work this out. The internet wasn’t much help either. Anyone out there any suggestions for me? I’m kinda stuck…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I’d start with the ‘pre_insert_term’ filter. Unless the current user has some admin specific capability to bypass this filter, the filter would verify that the taxonomy is for category and that the parent ID is one of your 6 top level categories. If the parent check fails, create a new WP_Error object to be returned instead of initially passed term object. One error type that is already established is created like so:
    $term = new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
    You can create your own error class and message as well, but it’s nice to use something established when possible.

    That will effectively enforce your restrictions, but the user experience would be improved if only the allowed terms were in the parent dropdown when creating new categories. There’s a few places this occurs, and I’m unsure if there’s a way to filter these elements. Find the code that outputs the dropdowns, if there’s not a filter right there, functions called to get the terms might provide a filter. If all else fails, jQuery can be used to alter the dropdown options.

    Thread Starter AWKampen

    (@awkampen)

    Wow, this is way over my head, I’m afraid.
    But, I’ll try to figure out what that pre_insert_term filter is and how it works.
    I’ll get back to it!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict categories to 1 level only’ is closed to new replies.