0
votes

I have added a custom taxonomy to Users - user categories.

Using the code below, I am able to output the custom taxonomies in each edit-user profile page:

function show_user_category( $user ) {

//get the terms that the user is assigned to
$assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
$assigned_term_ids = array();
foreach( $assigned_terms as $term ) {
    $assigned_term_ids[] = $term->term_id;
}

//get all the terms we have
$user_cats = get_terms( 'user_category', array('hide_empty'=>false) );

echo "<h3>User Category</h3>";

//list the terms as checkbox, make sure the assigned terms are checked
foreach( $user_cats as $cat ) { ?>
    <input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]"  value="<?php echo $cat->term_id;?>"/>
    <?php
    echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
    echo '<br />';
}
}

The code above simply displays a list of checkboxes, ordered by term_id.

Of course, I want to display them in the same way that custom taxonomy terms would be displayed in a custom post type (a scrollable list of checkboxes, with child terms indented and underneath their parent term). The code above does not display the terms in order of parents/children.

Is there a WP function I can pass my taxonomy & terms to, to create what I described in the paragraph above? Or do I have to do it manually?

Thanks

1
In cases like this, I snoop the core WP code. You could / should too.random_user_name

1 Answers

0
votes

Short answer:
Yes, there are. They are called post_categories_meta_box() and post_tags_meta_box(), depending on the type of taxonomy you are using.

Long answer:
This is the sort of question that can be answered by inspecting the WP core code.

Here's the walk-through on how I did it to answer this question:

Editing a page in admin results in a url of wp-admin/post.php. So, inspect that file (post.php).

A quick search in that file for "tax" results in nothing, so we can at least look at the case 'edit' section, because we know we're doing an edit. And we can see it includes a file edit-form-advanced.php.

So, inspect that file (edit-form-advanced.php)

A quick search for "tax" gets into an area that looks promising.

Here we see they call get_taxonomy, and then that get_taxonomy object that is returned has a callback called meta_box_cb - so do a cross-file search for that. We find several instances, but the instance in taxonomy.php is the most promising (because of the file name), so lets look there.

It appears (around line 430 of taxonomy.php) that there's a pair of callback functions - post_categories_meta_box and post_tags_meta_box that WP core is using. Let's check them out - do a cross-file search for function post_categories_meta_box, and we find both of these functions exist in the file meta-boxes.php. The comments for the functions are excellent, and tell us what you are asking: "Display post categories form fields".

Armed with this information, we can ask Google, and we find the WP documentation for the functions:

post_categories_meta_box

and

post_tags_meta_box