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
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$smallestand$largestvalues. Accepts any valid CSS text size unit.$numberintdefault: 0The 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 sortedValue 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: 1Whether to enable filtering of the final output via 'wp_generate_tag_cloud'.$topic_count_textarraydefault: nullNooped plural text from _n_noop() to supply to tag counts.$topic_count_text_callbackcallabledefault: nullCallback 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 equivalentsWhether 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
- Scaling
- Scales with input
- Instructions
- 96–125
- Plugin surface
- 3 hooks
- Called by
- 3
Touches nothing outside its own arguments.
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 319.
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.
3 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
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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && !$args | 96–110 | wp_parse_args(), _n_noop(), apply_filters(), uasort(), min(), max(), apply_filters() |
!empty($tags) && !isset($args) && empty($args) && !empty($tags_sorted) && $tags_sorted === false && !$args | 101–115 | wp_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 && $args | 102–116 | wp_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 && !$args | 103–117 | wp_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 && $args | 107–121 | wp_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 && !$args | 108–119 | wp_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 && $args | 109–123 | wp_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 && $args | 114–125 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 318 | 96–125 | 33 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 319 | 96–125 | 33 | |
| 8.4 | 319 | 96–125 | 33 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 328 | 96–126 | 33 | |
| 8.2 | 328 | 96–126 | 33 | 1 more instruction than PHP 8.1 |
| 8.1 | 327 | 96–126 | 33 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 330 | 96–126 | 33 |
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:
- apply_filters( tag_cloud_sort )filterline 900 (+55 into the body)
Filters how the items in a tag cloud are sorted.
- apply_filters( wp_generate_tag_cloud_data )filterline 999 (+154 into the body)
Filters the data used to generate the tag cloud.
- apply_filters( wp_generate_tag_cloud )filterline 1054 (+209 into the body)
Filters the generated output of a tag cloud.
Uses · 8
- wp_parse_args()Merges user defined arguments into defaults array.
- _n_noop()Registers plural strings in POT file, but does not translate them.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- translate_nooped_plural()Translates and returns the singular or plural form of a string that's been registered with _n_noop() or _nx_noop().
- number_format_i18n()Converts float number to format based on the locale.
- esc_attr()Escaping for HTML attributes.
- esc_url()Checks and cleans a URL.
- esc_html()Escaping for HTML blocks.
Used by · 3
- install_dashboard()Displays the Featured tab of Add Plugins screen.
- wp_ajax_get_tagcloud()Handles getting a tagcloud via AJAX.
- wp_tag_cloud()Displays a tag cloud.
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.
Signature, return type and hooks compared across 5 parsed releases.
show_count argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.