The MU forums have moved to WordPress.org

[Hack] Community Tag cloud for Mu-1.3 (18 posts)

  1. peeloo
    Member
    Posted 16 years ago #

    Hi all,

    Because nothing easy was ready to be install to have a community tag could using last Wordpress 1.3 tags, I've added a new function in wpmu-functions.php

    Read the code on my blog, or at the end of this post.

    Please tell me what you think of it, and if you find any bugs.

    Cheers,

    Alex

    function get_community_tag_cloud( $display = false, $limit = 50, $font_size = 20 ) {
    	global $wpdb;
    
    	// Get all blogs
    	$sites = $wpdb->get_results( "SELECT blog_id FROM wp_blogs", OBJECT);
    	$array_tags = array();
    
    	// Set category to not display, usually 'Uncategorized'
    	$no_category = "Sin Categorizar";
    
    	foreach($sites as $site){
    		$table_tags = "wp_".$site->blog_id."_terms";
    		$table_taxonomy = "wp_".$site->blog_id."_term_taxonomy";
    		if($exists = $wpdb->get_row("show tables like '$table_tags'")){
    			// Get name and count for each post_tag
    			$tag_results = $wpdb->get_results("SELECT name, count FROM $table_tags INNER JOIN $table_taxonomy ON $table_tags.term_id = $table_taxonomy.term_id WHERE $table_taxonomy.taxonomy LIKE 'post_tag'");
    			foreach ($tag_results as $tag) {
    				if( strlen($tag->name) > 0 and $tag->name != $no_category){
    					// Increment count of this tag
    					$array_tags[$tag->name] += $tag->count;
    				}
    			}
    		}
    	}
    
    	// Sort tag array by 'popularity'
    	arsort($array_tags, SORT_NUMERIC);
    
    	// Limit size if necessary
    	if(count($array_tags) > $limit){
    		$array_tags = array_slice($array_tags, 0, $limit);
    	}
    
    	// Display options
    	if( $display == true ){
    		if( is_array( $array_tags ) ) {
    			reset( $array_tags );
    			echo "<div id='tag_cloud'>";
    			foreach ( (array) $array_tags as $name => $count ) {
    				$url = get_site_option('url')."/tags/$name";
    				echo "<span style='font-size:".$font_size."px'><a href='$url'>$name</a></span> ";
    				if($font_size > 8){
    					$font_size--;
    				}
    			}
    			echo "</div>";
    		}
    	}
    
    	return $array_tags;
    }
  2. Konstan
    Member
    Posted 16 years ago #

    Getting an error: syntax error, unexpected T_STRING

    Edit: error gone after changing some " to '
    Edit 2: nothing shows up. Just doesnt work.

  3. peeloo
    Member
    Posted 16 years ago #

    Working here, have you take care of the display flag?

    get_community_tag_cloud($display=true);

  4. Konstan
    Member
    Posted 16 years ago #

    Can you post the url where you have this working? My text editor isnt liking your code, the colors are all wrong.

    Edit: there, I managed to get it to work after I edited all the weird looking ", probably because of the theme's font, the editor wasnt liking them. Now the only problem is that when you click on a link you dont get the post list.

  5. peeloo
    Member
    Posted 16 years ago #

    I get it working on a local install, and on a private demo we currently have for a client, sorry I can't make a more public demo.

    It's installed like this:

    1) copy function get_community_tag_cloud(...){...} in your /wp-include/wpmu-functions.php
    2) add get_community_tag_cloud($display=true); in your theme

    EDIT: oups, my blog format tools has fucked up quotes everywhere :/ Now corrected, and code inside a txt file.

    EDIT2: yes, I havn't look to tag url yet, just new on MU and don't know how tags permalink is working

  6. Konstan
    Member
    Posted 16 years ago #

    You can take a look at http://code.danalog.nl/danalog-wpmu-site-wide-tags.txt

    Tag cloud shows up now, at my test server at http://bh.kostya.cl

  7. Konstan
    Member
    Posted 16 years ago #

    Is there a way to make the tags show up randomly and not in order?

    I've tried to modify but only numbers appear instead of the actual tag words.

    Anyway, I'll be watching this thread and help you test it since its an addition I really really need for my site.

  8. lunabyte
    Member
    Posted 16 years ago #

    You do realize that with any decent amount of sites that this is going to be really, really, really resource intensive, right?

  9. Konstan
    Member
    Posted 16 years ago #

    Are you using the separate WP install solution? Does it work fine with 1.3?

  10. peeloo
    Member
    Posted 16 years ago #

    lunabyte: would it be possible to cache an array with wp_cache_add ?

    Konstan: There should be a way to put them in random, but you'll have to take care of the font-size to do so. Tell me if you find any solution for this ;-)

  11. lunabyte
    Member
    Posted 16 years ago #

    @ Konstan: I used to, but ended up writing my own complete system.

    @peeloo: Possible? Yes. It would be better than running it every time, but think of it like this: Every time that function is run, it's going to run 2 queries for every blog in the system. For a couple dozen blogs, it isn't noticeable. For hundreds, thousands, or tens of thousands... see the problem? 500 blogs = 1000 queries, plus the initial query for the list of blogs.

    At that point, even using caching and only running it once every so often is still going to put a massive strain on a large site.

  12. Konstan
    Member
    Posted 16 years ago #

    I can see how this would put a big strain on a server...I only have around 2k users but that still would be a lot.

    On the other hand, I really want this feature for some plans I have for my site.

  13. lunabyte
    Member
    Posted 16 years ago #

    "I only have around 2k users but that still would be a lot."

    Some 4000 queries every time that function runs. On top of timeout issues, it could spike the server load, bog down the CPU and RAM, and if you're on shared hosting it would probably get you the boot pretty quick.

  14. peeloo
    Member
    Posted 16 years ago #

    Konstan: have you tested this method on your 2k user install to see the performance?

    I'm trying to find a solution about this, maybe using a cron once a day to manage this task outside apache/php stack, but then goodbye plugin.

  15. Konstan
    Member
    Posted 16 years ago #

    No, havent tested on my live site.

    And lunabyte, how does you system work? Similar to the other wp install solution or something different?

  16. tlembke
    Member
    Posted 16 years ago #

    Thanks for the script, which generates a tag cloud well, for me.

    However, when a tag in the cloud is clicked, it doesn't take you to a valid page, at least as far as I can see.
    - tags/tagname doesn't find the tags from the other blogs.

    Or am I missing something?

  17. peeloo
    Member
    Posted 16 years ago #

    Hi tlembke,

    Yes, i'm still looking for a solution to make a sitewide tag search...

    Current solution seems to bring the same performance problem: rescan all blogs tables in database and select tag before to display result page.

    Feel free to comment if you have any idea about how to do this kindly :)

  18. au8ust
    Member
    Posted 16 years ago #

    Nice hack :D

    Hope to see the sidewide tag for this.

About this Topic