Making Sitewide Tags work

Sitewide Tags is a cool plugin by Donncha O Caoimh that pulls blog posts from all over a WordPress Multi-User installation – like the one here on the CUNY Academic Commons – into a supplementary catch-all blog. The power of this plugin is that, with all sitewide blog posts aggregated into one place, you can begin to see the kinds of topics and trends that emerge from the community of bloggers. More specifically, Sitewide Tags allows you to create a tag cloud that reflects blogging activity across the entire community. (See the tag cloud at WordPress.com for a sense of what this looks like.)

I’ve got Sitewide Tags up and running here on the Commons – see our tag cloud (scroll down the page) and our aggregated blog. Getting things running seamlessly took a bit of tinkering though, and I thought it might be useful to share some of the tinkering here.

Read on for more of this (unexpectedly!) long process.

Theming and navigation

The appearance of the Commons’s main page is determined by a fairly heavily-modified version of the WPMU Nelo theme. The first step to integrating the tags blog into the rest of the site was to apply the Nelo style. Simply applying the Nelo style wholesale doesn’t work right, for a few reasons that I’ll discuss; the tags blog’s theme needs some modification. There are really two ways to accomplish this: (1) apply the same Nelo theme to the tags blog and then create new page templates that are customized for the tags blog, or (2) copy the Nelo theme, rename it (in the header of style.css), and make the necessary modifications to that theme’s template. Option 1 is probably the more streamlined and upgrade-friendly route, but of course I didn’t really think of this until I had pretty much finished the customization, so I went with option 2.

Perhaps the most important modification I made to the nelo-for-tags theme (as I, in a fit of extreme creativity, dubbed the copied skin) has to do with the navigation. On the main site, the nav buttons that appear directly below the CUNY Academic Commons logo are links that point to WP pages within that blog. An easy way to copy the nav into nelo-for-tags would be to copy and paste the nav markup into nelo-for-tags/header.php (in place of the wp_list_pages command) , but this is not very future-friendly: if we change the name or order of any nav pages in the future, we’d have to apply those changes manually to the tags blog. A bit of Googling led me to a solution. Dusty Reagan wrote a function that he calls wp_list_main_pages that pulls the navigation dynamically from the main site blog onto any other blog on the site. Brief instructions:

  • place the function into nelo-for-tags/functions.php
  • change the number in $wpdb->set_blog_id(1); (near the end of the function) to the id of your main blog – in our case, it was 1, which means that no change was necessary
  • in nelo-for-tags/header.php, change wp_list_pages to wp_list_main_pages

A few things need to be done to clean up the nav. First: wp_list_main_pages imports nav links as relative links, which means that (for example) “Groups” points to https://tags.commons.gc.cuny.edu/groups instead of the correct https://commons.gc.cuny.edu/groups. So I added a line to wp_list_main_pages, immediately after the line that reads $output = str_replace("pressroom/", "", $output);, that looks like this:

$output = str_replace('http://tags.commons', 'http://commons', $output);

In other words, replace every instance of (the incorrect) ‘http://tags.commons’ in the nav bar to ‘http://commons’. Next, because wp_list_main_pages makes the nav import work by tricking WP into thinking that the current blog (tags) is actually the main blog, we need to end the ruse if we want further self-reference to work on the page. Immediately after the line just added, add another line:

$wpdb->set_blog_id(28);

where 28 is the number of your tags blog. Finally, there are some remaining places in the nav bar – places, in particular, where the nav structure is not determined by the pages on the main blog – where the href must be changed manually. Replace instances of <?php bloginfo('url'); ?> in header.php with the URL of the main blog (https://commons.gc.cuny.edu in our case). This final step is perhaps not all that elegant, but it’s far less likely that the URL of the home page will change in the future than that the main blog’s nav pages will change.

Other changes to the tags theme

On an aggregating blog, it’s helpful to include different information than what is included in the standard page template of a regular blog.

  1. Blog title – Because the posts on the page will come from many different blogs, it’s helpful to include “From the blog…” information alongside the title of each post. Two things need to happen for this information to appear: (a) you have to make sure that the required data (the title and URL of the post’s original source blog) gets written to the tag blog’s db table; and (b) you have to call that information into the template. Here’s what I cobbled together:
    1. Open up the Sitewide Tags plugin (stored at /wp-content/plugins/wordpress-mu-sitewide-tags/sitewide-tags.php, if you’ve already installed it). Lines 179-183, or thereabouts, should read like this:

      $post->ping_status = 'closed';
      $post->comment_status = 'closed';
      $p = wp_insert_post( $post );
      add_post_meta( $p, "permalink", $permalink );

      Replace these lines with the following code:

      $post_blog_table = 'wp_' . $post_blog_id . '_options';

      $global_post_source = $wpdb->get_row( "SELECT * FROM $post_blog_table WHERE `option_name` = 'siteurl'" );
      $source_blog_uri = $global_post_source->option_value;



      $global_post_source = $wpdb->get_row( "SELECT * FROM $post_blog_table WHERE `option_name` = 'blogname'" );
      $source_blog_name = $global_post_source->option_value;


      $post->ping_status = 'closed';
      $post->comment_status = 'closed';


      $p = wp_insert_post( $post );
      add_post_meta( $p, "permalink", $permalink );
      add_post_meta( $p, 'siteurl', $source_blog_uri);
      add_post_meta( $p, 'blogname', $source_blog_name);

      The short story of this code: every time Sitewide Tags kicks in (which is whenever a blog post is published across the site), the plugin goes to the source blog’s table, finds the blog’s URL and title, and writes it as metadata to the new post in the tag blog’s table.

    2. Next, open up nelo-for-tags/page.php (or whatever your theme directory is) and find the place where you want the “From the blog…” information to appear. (I like it right below the title of a given post – see our tags blog to see my positioning.) Insert the following code:


      <?php if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter )
      $post = $id;
      else
      $post = &get_post($id);


      $post_id = $post->ID;

      if (get_post_meta($post_id, 'siteurl', true)) {
      $source_url = get_post_meta($post_id, 'siteurl', true);
      $source_blog = get_post_meta($post_id, 'blogname', true);


      echo '<h2 class="tags-blog-title">From the blog <a href="' . $source_url . '">' . $source_blog . '</a></h2>';
      }?>

    Keep in mind that your tags blog will only show the source blog data for posts that are created after this point – the plugin does not go back and find source blog data for existing posts. So you’ll have to create new posts to test this.

  2. Tag-specific page titles – In nelo-for-tags/home.php – the template that creates the main page at https://tags.commons.gc.cuny.edu – I added a page header just after the line <div id="post-entry">, so that visitors would know what this site was all about:

    <h1>Sitewide Posts</h1>
    <h3>New blog posts from across the CUNY Academic Commons</h3>

    For nelo-for-tags/index.php, which creates the pages corresponding to specific tags (like this), I thought it would be helpful to include the tag itself in this header. So I inserted the following instead:

    <h1>Sitewide Posts - <em><?php wp_title(''); ?></em></h1>
    <h3>New blog posts from across the CUNY Academic Commons</h3>

Tweaking the tag cloud

I wanted the sitewide tag cloud to be widgetized, so that we’d be able to put it on our News page, as well as anywhere else on the site that we might like. Sitewide Multi Widget by dsader is a flexible way to get this done. Once you’ve activated Sitewide Multi Widget, you can add it through Dashboard > Appearance > Widgets to any blog on the site.

I made a tweak to Sitewide Multi Widget code so that it would display all tags in the cloud, instead of the default 25 or 40 or whatever it was. Here’s how: Find the line that includes wp_tag_cloud(); (around line 112). The WordPress Codex URL in the comment http://codex.wordpress.org/Template_Tags/wp_tag_cloud has all the details about this function’s options. I changed mine to

wp_tag_cloud('number=0');

which displays all tags.


As it’s now configured, I think that the tags blog, and the sitewide tag cloud that it supports, is poised to be a real discovery engine for members of our community. I can envision putting the tag cloud widget in lots of places all over the site. I can also envision getting more information into the tags database – once BuddyPress supports tags natively, for instance.

I hope that someone out there can get some use from some of this information!

13 thoughts on “Making Sitewide Tags work”

  1. Hi George. It’s already up and running on the Commons. Visit the news page http://commons.gc.cuny.edu/news/ and scroll down: you’ll see the tag cloud on the right hand side. Click on any of those tags to be taken to the corresponding page in the tag blog, where you’ll see all of the posts from around the site that have been appropriately tagged.

    I know Matt has been talking about making this kind of tag cloud more visible on the front page of the Commons. If you have more ideas about where this kind of information would benefit members of the Commons community, I hope you’ll let us know.

  2. and . . . . we’re a go! The tag cloud is now on the front page. Thanks so much to Boone for his excellent work on this. As this post shows, it wasn’t easy.

    In the future, we’re going to try to create a tag cloud that reflects content from all parts of the site (wiki pages, blog posts, forum posts). That’s definitely something we’ll be working towards.

  3. Hey Boone,

    Great post! Must bookmark!
    I do have one problem with the sitewide tags plugin.

    It would be nice to exclude certain blogs from sitewide aggregation via an array of blog IDs.

    Do you have any pointers?

  4. Hi Ray,

    I haven’t tried to do this myself, but taking a quick look at the code, here’s the advice I can offer.

    When the plugin is activated sitewide, the function sitewide_tags_post is hooked to every post save. Around line 146-7 there is a command that checks to see whether a blog is public, and if it’s not, then it ends the function (return;). You could add a few lines around here that do something similar, except by checking against a preset array rather than the “public” metadata. Try something like this:
    $exception_array = array(5, 15, 37) // (where 5, 15, and 37 are blogs that are left out)
    if ( in_array( $post_blog_id, $exception_array) )
    return;

    I haven’t tested this code, but something very close to it should work, in theory. Let me know if you get it running!

  5. Hey Boone,

    Thanks for those pointers. I’ll have to try that piece of code out.

    Still debating where to best utilize Sitewide Tags on one of the WPMU sites I’m working on. But this is a step in the right direction.

    Will report back when I have a chance to play around with Sitewide Tags.

  6. If anyone is still reading this, what version of WPMU did this use? I’m on 2.8.4. and the code to insert to blogname and url into the tag blog database just does…..nothing.

    I’ve not worked with MU in around 2 years so I don’t know if it’s a version thing or me being a fool.

  7. Hi Michelle,

    I’m pretty sure I developed this method on WPMU 2.8.4, so it should work.

    How do you know that the blogname, etc metadata is not being stored in the database? It should be in the postmeta table for the tags blog (something like wp_75_postmeta, though I’m not near my development machine so I’m saying this from memory!). Look for meta_keys called blogname, siteurl, and postmeta corresponding. If you find any, it means that the code is working. If not, then perhaps you’ve put the code in the wrong place, so that it’s not actually being called when a new post is posted to the sitewide tags blog?

  8. Boone, just revisting this blog post.

    Going to implement sitewide tags as a search engine for all WPMU blogs on a site I’m working on.

    That wp_list_main_pages() function you listed looks ever so useful! Thanks for posting a link to that. I was using some weird AJAX stuff, which wasn’t nice at all.

  9. Boone, it would be great if you could update this post after looking at the latest version of the sitewide tags plugin (v0.4.1.1).

    Your references to Lines 179-183, or thereabouts and the code in the latest version of the script barely resembles it. You seem to be the only person who’s posted any code revisions to this plugin, and I’ve been using your logic as a basis for passing custom taxonomy terms through as metadata (Don’t ask why I don’t just use a filter on the custom taxonomy function in the plugin – I tried, but cannot make it work despite Ron’s sage-like advice).

    Anyway, just thought I’d let you know that there are probably other people like me that are using this post as a framework to guide their own changes, so it might be worth updating to reflect v0.4.1.1.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image