Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to change tag tooltip to tag Description?

I tried and my code is mentioned below.

function title_text( $description ) {
    return sprintf( _n('%s topic', '%s', $description), $description );
}

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

This is not working. Can any one please check this code and find me a correct solution?

share|improve this question
1  
It is not clear from your example: do you want another representation for the number, or do you want to use the term description? –  toscho Jan 6 '13 at 14:45
    
I need term description –  raghu Jan 13 '13 at 5:29
    
Did my answer solve your problem? –  toscho Mar 18 '13 at 22:46
add comment

1 Answer

The argument topic_count_text_callback cannot do what you need, because it doesn’t get the term ID and the taxonomy as arguments. This should be fixed in core. I think I will write a patch for that later.

Update: I have written a patch for Ticket #21198. The milestone is 3.6, so the following answer will be out of date eventually. I will update this post then.

But not all hope is lost. We can filter the generated markup on wp_tag_cloud, run a regex on that and extract the term ID from the class attribute and the taxonomy from the second parameter $args. Then we use the term ID and the taxonomy to get the term description and replace the original title attribute.

add_filter(
    'wp_tag_cloud', # filter name
    array ( 'WPSE_78426_Tag_Cloud_Filter', 'filter_cloud' ), # callback
    10, # priority
    2   # number of arguments
);

/**
 * Replace title attribut in a tag cloud with term description
 *
 * @author toscho http://toscho.de
 */
class WPSE_78426_Tag_Cloud_Filter
{
    /**
     * Current taxonomy
     *
     * @type string
     */
    protected static $taxonomy = 'post_tag';

    /**
     * Register current taxonomy and catch term id per regex.
     *
     * @wp-hook wp_tag_cloud
     * @uses    preg_callback()
     * @param   string $tagcloud Tab cloud markup
     * @param   array  $args Original arguments for wp_tag_cloud() call
     * @return  string Changed markup
     */
    public static function filter_cloud( $tagcloud, $args )
    {
        // store the taxonomy for later use in our callback
        self::$taxonomy = $args['taxonomy'];

        return preg_replace_callback(
            '~class=\'tag-link-(\d+)\' title=\'([^\']+)\'~m',
            array ( __CLASS__, 'preg_callback' ),
            $tagcloud
        );
    }

    /**
     * Replace content of title attribute.
     *
     * @param array $matches
     *        $matches[0] = complete matched string,
     *        $matches[1] = term id,
     *        $matches[2] = original content of title attribute
     * @return string
     */
    protected static function preg_callback( $matches )
    {
        $term_id = $matches[1];
        // get term description
        $desc = term_description( $term_id, self::$taxonomy );
        // remove HTML
        $desc = wp_strip_all_tags( $desc, TRUE );
        // escape unsafe chacters
        $desc = esc_attr( $desc );

        // rebuild the attributes, keep delimiters (') intact
        // for other filters
        return "class='tag-link-$term_id' title='$desc'";
    }
}

Result

enter image description here

share|improve this answer
    
Thanks, It works! –  raghu Jan 15 '13 at 7:23
    
@raghu Click the check mark to mark this answer as the solution, so later readers know this worked for you. :) –  toscho Jan 15 '13 at 15:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.