Add ‘data-text’ attribute to TagCloud HTML


I am trying to add a data-text attribute to the selected tag in the TagCloud widget. I want to do this to highlight the selected tag. I have so far managed to add a class to it, but the reason I need to add data-text is to copy the tag text into data-text so I can set the content in before and after elements.

Here is what I have :

add_filter( 'wp_generate_tag_cloud_data', function( $tags_data ) {
    $body_class = get_body_class();
    foreach( $tags_data as $key => $tag ) {
        if( in_array( 'tag-'.$tag['id'], $body_class)) {
            $tags_data[$key]['class'] =  $tags_data[$key]['class'] ." active-tag";
            $tags_data[$key]['data-text'] = $tag['name']; // This doesn't work
    return $tags_data;

How can I access the tag’s HTML to add this data-text attribute ?
Thank you !

, wqt 4 years 2019-11-01T07:30:03-05:00 0 Answers 75 views 0

Leave an answer