wp_terms_checklist( int $post_id = 0, array|string $args = array() ): string
- Since
- 3.0.0, 4.4.0
- Source
wp-admin/includes/template.php:81
Description
Taxonomy-independent version of wp_category_checklist().
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$post_idintoptional- Post ID. Default 0.Default:
0 $argsarray|stringoptional- Array or string of arguments for generating a terms checklist. Default empty array.Default:
array()$descendants_and_selfintdefault: 0ID of the category to output along with its descendants.$selected_catsint[]default: falseArray of category IDs to mark as checked.$popular_catsint[]default: falseArray of category IDs to receive the "popular-category" class.$walkerWalkerdefault: empty which results in a Walker_Category_Checklist instance being usedWalker object to use to build the output.$taxonomystringdefault: 'category'Taxonomy to generate the checklist for.$checked_ontopbooldefault: trueWhether to move checked items out of the hierarchy and to the top of the list.$echobooldefault: trueWhether to echo the generated markup. False to return the markup instead of echoing it.
Return value
string- HTML list of input elements.
Performance profile
How much work a call to wp_terms_checklist() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 78–121
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via wp_get_object_terms().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.
Third-party callbacks on 'wp_terms_checklist_args' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
wp_get_object_terms()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 24 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_terms_checklist() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$parsed_args | 78–82 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), ->walk() |
!$parsed_args | 81–85 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), ->walk() |
!$parsed_args | 83–87 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), ->walk() |
!$parsed_args | 86–90 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), ->walk() |
!$parsed_args | 88–92 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), ->walk() |
!$parsed_args | 90–94 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 91–96 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), array_keys(), ->walk(), ->walk() |
!$parsed_args | 91–95 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), ->walk() |
!$parsed_args | 93–97 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 94–99 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk() |
!$parsed_args | 95–99 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 96–101 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), array_keys(), ->walk(), ->walk() |
12 further outcomes, up to 121 instructions
!$parsed_args | 98–102 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 99–104 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk() |
!$parsed_args | 100–104 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 101–106 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), array_keys(), ->walk(), ->walk() |
$parsed_args | 103–108 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
!$parsed_args | 103–107 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk() |
$parsed_args | 104–109 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk() |
$parsed_args | 106–111 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
$parsed_args | 108–113 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
$parsed_args | 111–116 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
$parsed_args | 113–118 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
$parsed_args | 116–121 | apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 157 | 78–121 | 11 | |
| 8.5 | 157 | 78–121 | 11 | |
| 8.4 | 157 | 78–121 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 160 | 78–121 | 11 | |
| 8.2 | 160 | 78–121 | 11 | |
| 8.1 | 160 | 78–121 | 11 | |
| 7.4 | 160 | 78–121 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_terms_checklist() runs, in this order:
- apply_filters( wp_terms_checklist_args )filterline 102 (+21 into the body)
Filters the taxonomy terms checklist arguments.
Uses · 8
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_args()Merges user defined arguments into defaults array.
- get_taxonomy()Retrieves the taxonomy object of $taxonomy.
- current_user_can()Returns whether the current user has the specified capability.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
- get_term()Gets all term data from database by term ID.
- Walker_Category_Checklist::__construct()
Used by · 4
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- _wp_ajax_add_hierarchical_term()Handles adding a hierarchical term via AJAX.
- post_categories_meta_box()Displays post categories form fields.
- wp_category_checklist()Outputs an unordered list of checkbox input elements labeled with category names.
Source code
function wp_terms_checklist( $post_id = 0, $args = array() ) { $defaults = array( 'descendants_and_self' => 0, 'selected_cats' => false, 'popular_cats' => false, 'walker' => null, 'taxonomy' => 'category', 'checked_ontop' => true, 'echo' => true, ); /** * Filters the taxonomy terms checklist arguments. * * @since 3.4.0 * * @see wp_terms_checklist() * * @param array|string $args An array or string of arguments. * @param int $post_id The post ID. */ $params = apply_filters( 'wp_terms_checklist_args', $args, $post_id ); $parsed_args = wp_parse_args( $params, $defaults ); if ( empty( $parsed_args['walker'] ) || ! ( $parsed_args['walker'] instanceof Walker ) ) { $walker = new Walker_Category_Checklist(); } else { $walker = $parsed_args['walker']; } $taxonomy = $parsed_args['taxonomy']; $descendants_and_self = (int) $parsed_args['descendants_and_self']; $args = array( 'taxonomy' => $taxonomy ); $tax = get_taxonomy( $taxonomy ); $args['disabled'] = ! current_user_can( $tax->cap->assign_terms ); $args['list_only'] = ! empty( $parsed_args['list_only'] ); if ( is_array( $parsed_args['selected_cats'] ) ) { $args['selected_cats'] = array_map( 'intval', $parsed_args['selected_cats'] ); } elseif ( $post_id ) { $args['selected_cats'] = wp_get_object_terms( $post_id, $taxonomy, array_merge( $args, array( 'fields' => 'ids' ) ) ); } else { $args['selected_cats'] = array(); } if ( is_array( $parsed_args['popular_cats'] ) ) { $args['popular_cats'] = array_map( 'intval', $parsed_args['popular_cats'] ); } else { $args['popular_cats'] = get_terms( array( 'taxonomy' => $taxonomy, 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false, ) ); } if ( $descendants_and_self ) { $categories = (array) get_terms( array( 'taxonomy' => $taxonomy, 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0, ) ); $self = get_term( $descendants_and_self, $taxonomy ); array_unshift( $categories, $self ); } else { $categories = (array) get_terms( array( 'taxonomy' => $taxonomy, 'get' => 'all',Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$echo argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/includes/template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.