WordPress.org

Ready to get started?Download WordPress

Forums

Change wp_tag_cloud() argument values using hook (2 posts)

  1. Jeff Cohan
    Member
    Posted 3 years ago #

    I'm using a child theme of the Thematic theme framework, but I think this is a general question.

    GOAL: I want to modify the values of some of the wp_tag_cloud() function arguments. Let's suppose I simply want to change the value of 'smallest' to 10 and 'largest' to 14.

    I do not want to touch the core function in category_template.php.

    Thematic has all of its proprietary hooks in its template files, so I don't see where I would hard-code the wp_tag_cloud() function without disrupting things that work.

    So I figure I can/should create a function in my child theme's functions.php file. I think I need to use either add_action(), add_filter() or apply_filters() to make this happen. I think I might need to use remove_[action | filter](), too.

    But that's all I (think I) know. None of my many different stabs worked.

    One stab was this (the result is echoing "Array" where the tag cloud should be):


    function tag_cloud_reformat() {

    $args = array('smallest' => 10, 'largest' => 14);
    return $args;
    }
    add_filter('wp_tag_cloud', 'tag_cloud_reformat');

    Help would be much appreciated.

    Jeff

  2. Rab Austen
    Member
    Posted 3 years ago #

    Hi Jeff,

    I've had the same issue as you and starting from your sample above I then decided to copy entire wp_tag_cloud() function into my themes functions.php and after some minor tweaks it seems to work great.

    Here's my entire function.php entry:

    // ---------- Hooking into Tag Cloud STARTS HERE
    
    function my_tag_cloud($defaults) {
    	$args = array(
    		'smallest' => 8, 'largest' => 16, 'unit' => 'pt', 'number' => 25,
    		'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
    		'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
    	);
    	$args = wp_parse_args( $args, $defaults );
    	$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
    
    	if ( empty( $tags ) )
    		return;
    
    	foreach ( $tags as $key => $tag ) {
    		if ( 'edit' == $args['link'] )
    			$link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
    		else
    			$link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
    		if ( is_wp_error( $link ) )
    			return false;
    
    		$tags[ $key ]->link = $link;
    		$tags[ $key ]->id = $tag->term_id;
    	}
    
    	$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
    
    	$return = apply_filters( 'my_tag_cloud', $return, $args );
    
    	if ( 'array' == $args['format'] || empty($args['echo']) )
    		return $return;
    
    	echo $return;
    }
    
    add_filter('wp_tag_cloud', 'my_tag_cloud');
    
    // ---------- Hooking into Tag Cloud ENDS HERE

    The subtle differences from original function are, swapping the $defaults as the passed in array and $args is my custom array (Although it's just a copy of the original defaults with some tweaks).

    The second important bit to note is the following line:

    $return = apply_filters( 'my_tag_cloud', $return, $args );

    Which needed to specify 'my_tag_cloud' instead of 'wp_tag_cloud'

    Anyway I'm only learning this stuff, so I may have made some huge blunder here but aesthetically it seems to work fine. Hope someone else with more experience can confirm if this is good, bad or indifferent and how I can improve it or perhaps make less bloated.

    Hope this helps you too
    Cheers
    Rab

Topic Closed

This topic has been closed to new replies.

About this Topic