Customize WordPress Tag Cloud Widget Functionality

This code enables you to modify the default functionality for the WordPress tag cloud widget. You’ll find the tag cloud widget by going to Appearance > Widgets.

The code uses the widget_tag_cloud_args filter which you can find around line 66 in wordpress > wp-includes > widgets > class-wp-widget-tag-cloud.php

Simply paste the following PHP code at the end of your child themes functions file.

add_filter( 'widget_tag_cloud_args', 'all_tag_cloud_widget_parameters' );
function all_tag_cloud_widget_parameters() {
    $args = array(
        'smallest' => 12, 
        'largest' => 18, 
        'unit' => 'pt', 
        'number' => 10,
        'format' => 'flat', 
        'separator' => "\n", 
        'orderby' => 'name', 
        'order' => 'ASC',
        'exclude' => '', 
        'include' => '', 
        'link' => 'view', 
        'taxonomy' => $current_taxonomy, 
        'post_type' => '', 
        'echo' => false
    );
    return $args;
}

Remove the arguments you do not want to modify from the array.

Example : If you only want to control which tags are included in the tag cloud widget, use the code like this:

add_filter( 'widget_tag_cloud_args', 'include_in_tag_cloud_widget' );
function include_in_tag_cloud_widget() {

    $include = array( 58, 59 );

    $args = array(
        'include' => $include,
        'taxonomy' => $current_taxonomy,
	    'echo' => false,     
    );
    return $args;
}

Where 58 and 59 are the i.d’s of the tags you want to include in the widget.

Exclude Tag by Name, I.D or Slug

You can also use the code like this to remove tags by name, id or slug using get_term_by :

add_filter( 'widget_tag_cloud_args', 'remove_tag' );

function remove_tag( $args ) {
    $args['exclude'] =  get_term_by( 'slug', 'your-tags-slug', 'post_tag' );
    return $args;
}

Swap out your-tags-slug in the above code with the slug for your tag.

Related Tutorials


Comments

8 responses to “Customize WordPress Tag Cloud Widget Functionality”

  1. Hi Brad!

    I’m trying to change the default number of tags. When using this code in the child-theme functions file, nothing changes, but if added in the main theme functions file, it works. Any clues about that?

  2. KONAN YAMADA Avatar
    KONAN YAMADA

    Sorry, I cannot display the title in spite of inputting title field in the widget.
    please help me.

    1. Brad Dalton Avatar
      Brad Dalton

      This works :

      [code]
      add_filter( ‘widget_tag_cloud_args’, ‘all_tag_cloud_widget_parameters’ );
      function all_tag_cloud_widget_parameters() {

      $args = array(
      ‘smallest’ => 10,
      ‘largest’ => 10,
      ‘unit’ => ‘pt’,
      ‘format’ => ‘list’,
      ‘order’ => ‘ASC’,
      );
      return $args;

      }
      [/code]

      1. KONAN YAMADA Avatar
        KONAN YAMADA

        Hello, I can solve using following code.
        —————-
        function all_tag_cloud_widget_parameters($args) {
        $myargs = array(
        ‘smallest’ => 1,
        ‘largest’ => 1,
        ‘unit’ => ’em’,
        ‘format’ => ‘list’,
        ‘order’ => ASC,
        );
        $args = wp_parse_args($args, $myargs);
        return $args;
        }
        add_filter( ‘widget_tag_cloud_args’, ‘all_tag_cloud_widget_parameters’ );
        —————-
        I used function “wp_parse_args”, It worked fine that merged old parameters and new parameters before return.

        1. Brad Dalton Avatar
          Brad Dalton

          The code is broken because it hasn’t been embedded correctly.

  3. KONAN YAMADA Avatar
    KONAN YAMADA

    Hello, I have a question.
    I implemented this code, then I can realise adjusting tag cloud parameter, but I cannot display the title.
    Currently, I use this code as a custom widget.
    Could you tell me how to display the title of tag list widget?

    1. Brad Dalton Avatar
      Brad Dalton

      Add the title to the widget title field

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.