Make WordPress Core

Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#21198 closed enhancement (fixed)

Add tag id to callback to allow customizing of the tooltip text in tag clouds

Reported by: najamelan's profile najamelan Owned by: ryan's profile ryan
Milestone: 3.6 Priority: normal
Severity: minor Version:
Component: Taxonomy Keywords: has-patch commit
Focuses: Cc:

Description

When hovering over tags in tag clouds wp shows the number of posts. To achieve this a callback is called with the number. This callback forms the text to be added to the title attribute.

The callback can be set by the user, but because it only gets the count, and not the tag id, not much can be done.

With the tag id the callback could for example return the tag description for the tooltip.

Not much is needed for this since the tag id is available in the context of the caller. A 2 line patch to wp-includes/category-template.php provides this functionality.

This is a patch generated by Git on the 3.4.1 code.

Subject: [PATCH] wp-includes/category-template: add tagid to call for tooltip

text in tag clouds

---
 wp-includes/category-template.php |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php
--- a/wp-includes/category-template.php
+++ b/wp-includes/category-template.php
@@ -559,7 +559,7 @@ function wp_tag_cloud( $args = '' ) {
  * @param integer $count number of posts with that tag
  * @return string text for the tooltip of a tag link.
  */
-function default_topic_count_text( $count ) {
+function default_topic_count_text( $count, $tag_id ) {
 	return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
 }
 
@@ -671,7 +671,7 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
 		$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
 		$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
 		$tag_name = $tags[ $key ]->name;
-		$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
+		$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count, $tag_id ) ) . "' style='font-size: " .
 			str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
 			. "$unit;'>$tag_name</a>";
 	}
-- 

Probably it would be desirable some day to replace the callback by a filter for consistency, but this works fine and probably has less overhead.

Thanks alot

Attachments (2)

21198.patch (866 bytes) - added by SergeyBiryukov 13 years ago.
21198.category-template.patch (837 bytes) - added by toscho 12 years ago.
Add two more arguments to topic_count_text_callback

Download all attachments as: .zip

Change History (16)

#1 @SergeyBiryukov
13 years ago

  • Keywords has-patch added

#2 @SergeyBiryukov
13 years ago

  • Component changed from General to Taxonomy

#3 follow-up: @azaozz
13 years ago

Generally it's not a good idea to use HTML IDs in any dynamically generated HTML. What if there are two tag clouds shown on the same page? There shouldn't be any invalid (non-unique) HTML IDs.

#4 in reply to: ↑ 3 @SergeyBiryukov
13 years ago

Replying to azaozz:

Generally it's not a good idea to use HTML IDs in any dynamically generated HTML.

Yes, but that's not what the ticket suggests (unless I'm missing something).

The patch just allows the callback function to receive the tag ID (numeric) and use it to provide more details (e.g. tag description) in the title attribute.

#5 @azaozz
13 years ago

Ah I see, my bad.

#6 @SergeyBiryukov
12 years ago

  • Milestone changed from Awaiting Review to 3.6
  • Type changed from feature request to enhancement

#7 @toscho
12 years ago

  • Cc info@… added

The term ID alone is not enough, we need at least the taxonomy too. The following patch does exactly this. It passes the complete list of arguments to the callback.

call_user_func( $topic_count_text_callback, $real_count, $tag_id, $args )

For a practical use case see this thread and the hilarious workaround I needed to achieve that.

With the patch we can use just:

wp_tag_cloud(
	array (
		'topic_count_text_callback' => 't5_desc_as_title'
	)
);

function t5_desc_as_title( $real_count, $term_id, $args )
{
	$desc = term_description( $term_id, $args['taxonomy'] );
	$desc = wp_strip_all_tags( $desc, TRUE );
	return esc_attr( $desc );
}

@toscho
12 years ago

Add two more arguments to topic_count_text_callback

#8 @ocean90
12 years ago

  • Cc ocean90 added

#9 @SergeyBiryukov
12 years ago

  • Keywords commit added

#10 @nacin
12 years ago

Should all of $args be passed? Seems to me like it could be possible for $args to be an array at some point, thus masking which taxonomy the $tag_id would be.

I would think that we should add $term_id, $taxonomy... And then maybe $args if it is still desired.

#11 @nacin
12 years ago

Or just the term object, if we have it at this point.

#12 @toscho
12 years ago

$args is an array, yes. It can be used to pass custom data into the function, so I thought it might be useful to pass it through to open room for creative things. For example, one could use two tag clouds for the same taxonomy with the same topic_count_text_callback and identify both by a custom instance parameter in $args.

The filter tag_cloud_sort uses $args too.

Oh, and once this is decided, I would like to remove the create_function() in wp_generate_tag_cloud(). But that’s a separate ticket, I guess …

#13 @ryan
12 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In 23741:

Pass tag ID and args array to topic_count_text_callback from wp_generate_tag_cloud().

Props najamelan, toscho
fixes #21198

#14 @nacin
12 years ago

In 23888:

Pass the tag object instead of just the tag ID to topic_count_text_callback. see [23741]. see #21198.

Note: See TracTickets for help on using tickets.