3 Ways to Change Font Size of Gutenberg Tag Cloud in WordPress (PHP, CSS, JS)


Do you use Tag Cloud in your WordPress websites?

The Tag Cloud widget shows a list of all the tags you’ve assigned to your posts. This widget is helpful because it gives your readers an at-a-glance view of your favorite subjects. In WordPress 5.0 or above, the WP Gutenberg block allows easily adding a simple tag cloud to posts and pages for displaying up to 45 of your most popular tags. The internal function ranks tag counts and calculates the font size of each tag accordingly. Each size is explicitly specified through the inline style of the link element. The more common tags will be displayed in the larger font size, and lets readers know what topics you write about most frequently.

Each tag is output as an HTML link element with a pattern that looks like:

HTML
  • {tag-url} – The URL links to the tag page
  • {id} – The taxonomy ID number of the tag
  • {pos} – The order of position in the tag cloud
  • {point-size} – The font size of the tag, which ranges between
    • minimum: 8 pt
    • maximum: 22 pt
  • {tag-name} – The name of the tag
  • {count} – The number of posts associating with the tag

Here’s an example of what the widget looks like:


How to Change the Font Size of Gutenberg Tag Cloud

Sometimes, the tags may look too big or too small to fit into your layout. Unfortunately, the Gutenberg block only possesses very limited options. There is no way for you to set the font size through the block panel of the Tag Cloud widget.

Nevertheless, you can change the font sizes of the Tag Cloud by inserting custom codes. The process can be implemented on either the server-side or the client-side. Three different ways are described and compared in the following tabs.

Modify with PHP (Server-side)

Here’s an example of what the modified widget looks like:

Source Code

Code Explanation

The above PHP code snippet adds a filter hook to modify the wp_tag_cloud() function. It applies the set_tag_cloud_sizes() function as the callback to be run when a tag cloud displays. Dragon’s function contains three major steps:

  1. Extract the tag items as $links by parsing the HTML link elements in the tag cloud.
  2. Add small and large classes to the tag items that are styled with the minimum (8pt) and maximum (22pt) font sizes, respectively.
  3. Remove the inline styling from all the tag items using Regular Expressions.

The smallest and largest tag items inherit the font sizes from the CSS classes. The rest tag items are in the normal font size. As a result, the font size is no longer proportional to the tag counts.

3 Ways to Change Font Size of Gutenberg Tag Cloud in WordPress (PHP, CSS, JS) 7

Modify to Equal Font Size

The first two steps are unnecessary If you just want to change all the tags to have the same font size. You can remove them from the set_tag_cloud_sizes() function, and just keep the codes for removing the inline styling and returning the result.

Advantages

  • Better page performance
    • Smaller page size
    • Less blocking time
    • No layout shift

Disadvantages

  • Possibly slow your WordPress server
  • Too complicated for beginners
  • Hard to customize the code with no PHP experience
  • Improper coding may produce security vulnerabilities

Modify with CSS (Client-side)

Here’s an example of what the modified widget looks like:

Source Code

CSS

Code Explanation

The graph below illustrates the quantized output yield by the CSS rules declared above.

3 Ways to Change Font Size of Gutenberg Tag Cloud in WordPress (PHP, CSS, JS) 8

It is flexible to divide the tag cloud into discrete subgroups. The key CSS selector pattern is a.tag-cloud-link[style*="font-size: {point-size}"], which selects all the tags that match the specified {point-size}. The*= operator allows the selector to also handle the decimal format effectively. Since Inline styling has the highest priority, it is necessary to override it by adding !important to the CSS rules. It replaces the font sizes of the tag items when the Tag Cloud elements are loaded on the client-side.

Advantages

  • Easy to implement and debug
  • 100% secure
  • Do not increase the server payload
  • No layout shift during page loading

Disadvantages

  • Slightly increase page load time

Modify with JavaScript (Client-side)

Here’s an example of what the modified widget looks like:

The Tag Cloud resizes during DOM rendering if using JavaScript to change its font sizes

Source Code

JS

Code Explanation

The JavaScript changes the inline styling of the tag items, when the script is fired on the client-side. The new font sizes are normalized based on the minimum font size and the specified scale factor. The following graph plots the relationship between the original and new sizes for the scale factors of 0.5 and 0.7, respectively.

3 Ways to Change Font Size of Gutenberg Tag Cloud in WordPress (PHP, CSS, JS) 9

Advantages

  • Easy to implement and debug
  • Parametric
  • No server-side security problems
  • Do not increase the server payload

Disadvantages

  • Increase page load time
    • Increase script evaluation time
    • Increase rendering time
  • Layout shift during page loading


FAQ

Tag Groups is the Advanced Way to Display Your Taxonomy Terms

Tag Groups is the Advanced Way to Display Your Taxonomy Terms

Tag Groups allows you to organize your WordPress taxonomy terms and show them in clouds, tabs, accordions, tables, lists and much more.

TaxoPress

POSSIBLE APPLICATIONS

  • Display your tags grouped by language or by topic.
  • Create an alphabetical index of your tags – think of a phone book for your tags.
  • Display only specific tags in your tag cloud.
  • Choose which tags to display in different sections of your blog.
  • Change the sorting order in your tag cloud.
  • Customize the links, the text, the font size or the separator of your tag cloud items.
  • Prepend or append to each tag a custom character or the post count.
  • Insert into each post a tag cloud using only the tags of this post.
  • Easily manage huge amounts of tags or posts in the backend by dividing them into groups.
  • Add a parent level to tags and make them hierarchical.
3 Ways to Change Font Size of Gutenberg Tag Cloud in WordPress (PHP, CSS, JS) 10
The smallest and largest font size settings for the Tag Group plugin’s Gutenberg block

This free plugins provides a number of advanced features for designing custom Tag Clouds in WordPress. I think this the best choice if you don’t want to touch any codes. You can easily adjust the font sizes of the tags with its Gutenberg block.

Nonetheless, I like adding custom codes rather than using this plugin. I don’t want to install too many plugins, as it will slow my WordPress and increase the risk of having security vulnerabilities. It’s overwhelming to use a feature-rich plugin, if you just simply need to change the font sizes. I only recommend it if you’re looking for superior and specially designed tag clouds.

You can also manually add a tag cloud to your WordPress using the wp_tag_cloud() function directly. You can insert the PHP snippet as a shortcode. It is perfect for creating a custom tag cloud page or for adding them to your theme layout. The official function has already contained many options, including sizes, orders, and formats. It is quite flexible to create custom tag clouds without adding a filter hook.

Default Usage

PHP

Example: Cloud limited in size and ordered by count rather than name

<?php wp_tag_cloud( 'smallest=15&largest=40&number=50&orderby=count' ); ?>

If you don’t know how to add PHP code to WordPress posts or pages, I recommend using the following plugin to make PHP snippets as custom shortcodes.

Insert PHP Code Snippet

Insert PHP Code Snippet

Add PHP code to your pages and posts easily using shortcodes.

xyzscripts.com


How do you use Tag Clouds on your websites? Any other clever tips we missed? Share them below!

If you like this post, please share it with your Facebook and Twitter. You might also support me by donating via Ko-fi.

Scroll to Top