Change tooltip in tag cloud

How to change tag tooltip to tag Description?

I tried and my code is mentioned below.

Read More
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?

Related posts

Leave a Reply

1 comment

  1. 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