Creating a WP tag cloud custom mod

Before you get deep into this article, I recommend you read the official documentation on WordPress(WP) tag cloud modifications on wordpress.org website. Here is a link to the specifics of what we will talk about in this article here. I have also extracted the default built in PHP functions which can be used to modify (alter) parameters and add functions to the default tag cloud.

  <?php $args = array(
    'smallest'                  => 8, 
    'largest'                   => 22,
    'unit'                      => 'pt', 
    'number'                    => 45,  
    'format'                    => 'flat',
    'separator'                 => \\"\n\\",
    'orderby'                   => 'name', 
    'order'                     => 'ASC',
    'exclude'                   => null, 
    'include'                   => null, 
    'topic_count_text_callback' => default_topic_count_text,
    'link'                      => 'view', 
    'taxonomy'                  => 'post_tag', 
    'echo'                      => true,
        'child_of'                   => null(see Note!)
); ?>

Out of all the possible parameters, today we will only focus on text size and spacing. I don’t think I have to go into detail about each and every one of the above since enough documentation already exists on official WP reference databases.

Expand upon our previous code

In the last tutorial we created an universal header for WP plugins. Now we will add function parameters to the tag cloud function, $args.

A function is like the type of MS office document (Word, Excel, etc) and a parameter is like a formatting directive (sum, align right, space, tab, etc). I picked, tag cloud function because it may the easiest to understand for novice programmer. The following code “attempt” modifies the text size and spacing between tags. The reason why I used “attempts” because the name of functions and parameters are arbitrary. We will talk about why PHP parameters are arbitrary in a different article.

<?php
/*
Plugin Name: Sanuja Custom Tags 
Plugin URI: https://sanuja.com/blog/creating-a-wp-tag-cloud-custom-mod/
Version: 1.2 Beta
Author: Sanuja Senanayake
Author URI: http://www.sanuja.com
Description: Changes the font sizes used by the tag cloud widget.
*/

function my_tag_cloud_filter($args = array()) {
   $args['smallest'] = 10;
   $args['largest'] = 15;
   $args['unit'] = 'pt';
   $args['separator'] = ' &nbsp; '
   $args['orderby'] = 'name';
   return $args;
}

add_filter('widget_tag_cloud_args', 'my_tag_cloud_filter', 100);
?>

You do not have to create a function, but a good programmers often use functions to avoid unnecessary repetition of code segments (there are many other benefits to functions we will discuss in a later post). You can create the same code without it using an array or with neither.

The $ sign indicates that the code is about to call in for a built in(pre configured) function immediately after it; args. Once args is called out, a parameter is defined within two square brackets [ ] often called a container. These brackets hold the information about a parameter within a set of quotations.

The assignment operator = separates the arguments on the left side from the right side manipulations. Any input typed on the right side will of the assignment operator will change the behaviors of the function’s parameter on the left side. For example, $args[‘smallest’] = 10 will change the parameter smallest in args function to 10. Note that this number ten has no default font size, type, etc factor. It can be changed using calling in other parameters for the same function (assuming it is supported).

An end of a parameter is dictated by the character ; which is immediately followed by a new line. You must always start a new parameter call in with new line and a $ sign.

If you decided to create your mod code in a function, make sure you use proper formatting. A proper format of a function always includes consistent indentations(number of spaces/tabs) for each line. While it is not necessary to return data in a function, a good programmer would often use rerun in every custom function.

Don’ forget to close the PHP code with ?> to avoid code run on. A run on code can create a database failure causing your WP install to crash. In an event of a crash, you can always update the files from the back end of your web server (HTTP/FTP).

Why create an independent code?

Explain why creating an independent code instead of modifying the core WP files is the better option here!