wppaste
WordPress

wp_generate_tag_cloud( WP_Term[] $tags, string|array $args = '' ): string|string[]

Since
2.3.0, 4.8.0
Source
wp-includes/category-template.php:845
Generates a tag cloud (heatmap) from provided data.

Compatibility

WordPress
since 4.8.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

$tagsWP_Term[]
Array of WP_Term objects to generate the tag cloud for.
$argsstring|arrayoptional
Array or string of arguments for generating a tag cloud.Default: ''
  • $smallestintdefault: 8 (pt)

    Smallest font size used to display tags. Paired with the value of $unit, to determine CSS text size unit.
  • $largestintdefault: 22 (pt)

    Largest font size used to display tags. Paired with the value of $unit, to determine CSS text size unit.
  • $unitstringdefault: 'pt'

    CSS text size unit to use with the $smallest and $largest values. Accepts any valid CSS text size unit.
  • $numberintdefault: 0

    The number of tags to return. Accepts any positive integer or zero to return all.
  • $formatstringdefault: 'flat'

    Format to display the tag cloud in. Accepts 'flat' (tags separated with spaces), 'list' (tags displayed in an unordered list), or 'array' (returns an array).
  • $separatorstringdefault: "\n" (newline)

    HTML or text to separate the tags.
  • $orderbystringdefault: 'name'. The {@see 'tag_cloud_sort'} filter can also affect how tags are sorted

    Value to order tags by. Accepts 'name' or 'count'.
  • $orderstringdefault: 'ASC'

    How to order the tags. Accepts 'ASC' (ascending), 'DESC' (descending), or 'RAND' (random).
  • $filterint|booldefault: 1

    Whether to enable filtering of the final output via 'wp_generate_tag_cloud'.
  • $topic_count_textarraydefault: null

    Nooped plural text from _n_noop() to supply to tag counts.
  • $topic_count_text_callbackcallabledefault: null

    Callback used to generate nooped plural text for tag counts based on the count.
  • $topic_count_scale_callbackcallabledefault: default_topic_count_scale()

    Callback used to determine the tag count scaling value.
  • $show_countbool|intdefault: 0. Accepts 0, 1, or their bool equivalents

    Whether to display the tag counts.

Return value

string|string[]
Tag cloud as a string or an array, depending on 'format' argument.

Performance profile

How much work a call to wp_generate_tag_cloud() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
96–125

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

Plugin surface
3 hooks

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

Called by
3

3 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

Further down the call graph this can also reach option, cache, serialize, query 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 · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_generate_tag_cloud() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && !$args96–110wp_parse_args(), _n_noop(), apply_filters(), uasort(), min(), max(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && !$args101–115wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_reverse(), min(), max(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && $args102–116wp_parse_args(), _n_noop(), apply_filters(), uasort(), min(), max(), apply_filters(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && !$args103–117wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_slice(), min(), max(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && $args107–121wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_reverse(), min(), max(), apply_filters(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && $tags && !$spread && !$font_spread && !$args108–119wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_reverse(), array_slice(), min(), max(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && $args109–123wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_slice(), min(), max(), apply_filters(), apply_filters()
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && $tags && !$spread && !$font_spread && $args114–125wp_parse_args(), _n_noop(), apply_filters(), uasort(), array_reverse(), array_slice(), min(), max(), apply_filters(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev31896–125331 fewer instruction than PHP 8.5
8.531996–12533
8.431996–125339 fewer instructions than PHP 8.3
8.332896–12633
8.232896–126331 more instruction than PHP 8.1
8.132796–126333 fewer instructions than PHP 7.4
7.433096–12633

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 · 3

3 hooks fire while wp_generate_tag_cloud() runs, in this order:

  1. apply_filters( tag_cloud_sort )filterline 900 (+55 into the body)

    Filters how the items in a tag cloud are sorted.

  2. apply_filters( wp_generate_tag_cloud_data )filterline 999 (+154 into the body)

    Filters the data used to generate the tag cloud.

  3. apply_filters( wp_generate_tag_cloud )filterline 1054 (+209 into the body)

    Filters the generated output of a tag cloud.

Uses · 8

Used by · 3

Source code

function wp_generate_tag_cloud( $tags, $args = '' ) {	$defaults = array(		'smallest'                   => 8,		'largest'                    => 22,		'unit'                       => 'pt',		'number'                     => 0,		'format'                     => 'flat',		'separator'                  => "\n",		'orderby'                    => 'name',		'order'                      => 'ASC',		'topic_count_text'           => null,		'topic_count_text_callback'  => null,		'topic_count_scale_callback' => 'default_topic_count_scale',		'filter'                     => 1,		'show_count'                 => 0,	); 	$args = wp_parse_args( $args, $defaults ); 	$return = ( 'array' === $args['format'] ) ? array() : ''; 	if ( empty( $tags ) ) {		return $return;	} 	// Juggle topic counts.	if ( isset( $args['topic_count_text'] ) ) {		// First look for nooped plural support via topic_count_text.		$translate_nooped_plural = $args['topic_count_text'];	} elseif ( ! empty( $args['topic_count_text_callback'] ) ) {		// Look for the alternative callback style. Ignore the previous default.		if ( 'default_topic_count_text' === $args['topic_count_text_callback'] ) {			/* translators: %s: Number of items (tags). */			$translate_nooped_plural = _n_noop( '%s item', '%s items' );		} else {			$translate_nooped_plural = false;		}	} elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {		// If no callback exists, look for the old-style single_text and multiple_text arguments.		// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural		$translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );	} else {		// This is the default for when no callback, plural, or argument is passed in.		/* translators: %s: Number of items (tags). */		$translate_nooped_plural = _n_noop( '%s item', '%s items' );	} 	/**	 * Filters how the items in a tag cloud are sorted.	 *	 * @since 2.8.0	 *	 * @param WP_Term[] $tags Ordered array of terms.	 * @param array     $args An array of tag cloud arguments.	 */	$tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );	if ( empty( $tags_sorted ) ) {		return $return;	} 	if ( $tags_sorted !== $tags ) {		$tags = $tags_sorted;		unset( $tags_sorted );	} else {		if ( 'RAND' === $args['order'] ) {			shuffle( $tags );		} else {			// SQL cannot save you; this is a second (potentially different) sort on a subset of data.			if ( 'name' === $args['orderby'] ) {				uasort( $tags, '_wp_object_name_sort_cb' );			} else {				uasort( $tags, '_wp_object_count_sort_cb' );			} 			if ( 'DESC' === $args['order'] ) {				$tags = array_reverse( $tags, true );			}		}	}

Changelog

Introduced in 2.3.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.8.0
Added the show_count argument.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/category-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.