wppaste
WordPress

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
Outputs an unordered list of checkbox input elements labelled with term names.

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: 0

    ID of the category to output along with its descendants.
  • $selected_catsint[]default: false

    Array of category IDs to mark as checked.
  • $popular_catsint[]default: false

    Array of category IDs to receive the "popular-category" class.
  • $walkerWalkerdefault: empty which results in a Walker_Category_Checklist instance being used

    Walker object to use to build the output.
  • $taxonomystringdefault: 'category'

    Taxonomy to generate the checklist for.
  • $checked_ontopbooldefault: true

    Whether to move checked items out of the hierarchy and to the top of the list.
  • $echobooldefault: true

    Whether 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

Reaches the database via wp_get_object_terms().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
78–121

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.

Plugin surface
1 hook

Third-party callbacks on 'wp_terms_checklist_args' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent querywp_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.

WhenInstructionsCalls it makes
!$parsed_args78–82apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), ->walk()
!$parsed_args81–85apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), ->walk()
!$parsed_args83–87apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), ->walk()
!$parsed_args86–90apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), ->walk()
!$parsed_args88–92apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), ->walk()
!$parsed_args90–94apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args91–96apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), array_keys(), ->walk(), ->walk()
!$parsed_args91–95apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), ->walk()
!$parsed_args93–97apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args94–99apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk()
!$parsed_args95–99apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args96–101apply_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_args98–102apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args99–104apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk()
!$parsed_args100–104apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args101–106apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), array_map(), taxonomy(), array_keys(), ->walk(), ->walk()
$parsed_args103–108apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk()
!$parsed_args103–107apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), get_term(), array_unshift(), ->walk()
$parsed_args104–109apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_merge(), wp_get_object_terms(), taxonomy(), taxonomy(), array_keys(), ->walk(), ->walk()
$parsed_args106–111apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), taxonomy(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk()
$parsed_args108–113apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), array_map(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk()
$parsed_args111–116apply_filters(), wp_parse_args(), get_taxonomy(), current_user_can(), array_map(), taxonomy(), taxonomy(), get_term(), array_unshift(), array_keys(), ->walk(), ->walk()
$parsed_args113–118apply_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_args116–121apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev15778–12111
8.515778–12111
8.415778–121113 fewer instructions than PHP 8.3
8.316078–12111
8.216078–12111
8.116078–12111
7.416078–12111

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:

  1. 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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Introduced the $echo argument.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.