• [It was recommended that I repost in this forum]

    I’m trying to set the max and min font size for a tag cloud made with the built in Gutenberg tag cloud block. Twenty Twenty theme.

    I found this code but it only works for me on the tag cloud widget in a widget area, not the tag block. I’ve tried removing the widget_ bit. I’ve tried changing that to block_. Neither worked.

    add_filter( 'widget_tag_cloud_args', 'change_tag_cloud_font_sizes');
    /**
     * Change the Tag Cloud's Font Sizes.
     *
     * @since 1.0.0
     *
     * @param array $args
     *
     * @return array
     */
    function change_tag_cloud_font_sizes( array $args ) {
        $args['smallest'] = '10';
        $args['largest'] = '18';
    
        return $args;
    }

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter bhkh

    (@bhkh)

    OK, OP here.

    I figured out that if I go to my copy of category-template.php
    (https://core.trac.www.ads-software.com/browser/tags/5.3/src/wp-includes/category-template.php#L0) I can modify the defaults on line 680 and it reduces just fine, as I would like.

    However, as I understand it, I really shouldn’t be going in and messing with that file.

    Any ideas to how to get the filter to work?

    I have tried replacing widget_tag_cloud_args in my code in the OP with wp_tag_cloud. This is there error that appears on the page:

    Fatal error: Uncaught TypeError: Argument 1 passed to change_tag_cloud_font_sizes() must be of the type array, string given, called in /home/xxxxxxxxxxx/wp-includes/class-wp-hook.php on line 290 and defined in /home/xxxxxxxxxxx/wp-content/themes/twentytwenty-child/functions.php:20 Stack trace: #0 /home/xxxxxxxxxxxx/wp-includes/class-wp-hook.php(290): change_tag_cloud_font_sizes('<a href="https:...') #1 /home/xxxxxxxxxxxxxxx/wp-includes/plugin.php(206): WP_Hook->apply_filters('<a href="https:...', Array) #2 /home/xxxxxxxxxxxxxxxxxxx/wp-includes/category-template.php(735): apply_filters('wp_tag_cloud', '<a href="https:...', Array) #3 /home/xxxxxxxxxxxxxxxxxxxx/wp-includes/blocks/tag-cloud.php(30): wp_tag_cloud(Array) #4 /home/xxxxxxxxxxxxxxxxxxx/wp-includes/class-wp-block-type.php(109): render_block_core_tag_cloud(Array, '') #5 /home/xxxxxxxxxxxxxxxxxxxx in /home/xxxxxxxxxxxxxxxxxxxxx/wp-content/themes/twentytwenty-child/functions.php on line 20

    I’ve taken out the specific directory and replaced with x’s.

    I’ve noticed the same issue… With trial and error i noticed the filter uses the output which you can filter. So you have to manipulate the html itself…

    My working example:

    add_filter('wp_tag_cloud','set_tag_cloud_sizes');
    
    function set_tag_cloud_sizes($args) {
        $links = explode('</a>', $args);
    
        // add classes
        foreach($links as $key => $link) {
          if(strpos($link, 'style="font-size: 8pt;"')) $link = preg_replace('/(class\w?=\w?".*?)"/', '$1 small"', $link);
          if(strpos($link, 'style="font-size: 22pt;"')) $link = preg_replace('/(class\w?=\w?".*?)"/', '$1 large"', $link);
          $links[$key] = $link;
        }
        $result = implode('</a>', $links);
    
        // remove inline styling
        $patterns = '/style=".*?"/';
        $replacements = '';
        $result = preg_replace($patterns, $replacements, $result);
    
        return $result;
    }
    • This reply was modified 4 years, 6 months ago by Dargno.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘What filter to change font size of tag cloud of standardGutenber block’ is closed to new replies.