Making Sitewide Tags work

Cross-posted at the CUNY Academic Commons Development blog

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 http://tags.commons.gc.cuny.edu/groups instead of the correct http://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 (http://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 http://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!

3 thoughts on “Making Sitewide Tags work

  1. Pingback: Teleogistic / Sitewide Tag Suggestion in Wordpress MU 2.8+

  2. AlanK

    Thanks, Boone, for making yourself available to respond to questions about this older blog.

    Ever since I found and installed the sitewide tag plugin, I’ve been trying to find out how to add the blog info to the output on the main page. The plugin code is not obvious to me, and the documentation is thin to nonexistent. I believe your article on this issue is perhaps the only one out there. I was pleased to find someone addressing this obvious issue.

    However, I’ve not had any success with the remedy. One thing that surprised me about your article is that the plugin is installed in the plugins directory. The documentation for installing the plugin is to drop it in the mu-plugins.

    In any event, I tried both directories and came a cropper. What I did was replace the four lines of code in the sitewide file with the code you listed under the heading, “Other changes to the tags theme”. I then introduced the code in the second part of that subhead to the index.php file in the wp default theme root directory. The result in each case was that the condition, if (get_post_meta($post_id, ‘siteurl’, true)), was not met and assignments in the statement were not made.

    Is this enough information for you to understand what might be going wrong?

    Reply
  3. Boone Gorges Post author

    Hi Alan. There are a couple things that might be going wrong.

    Check your database to see how your WP blog tables are named. My modifications assumes that they are of the form wp_5_posts, etc. (Bad practice on my part – I should rewrite these directions to reflect the correct way of doing things.) If you use a different database prefix from wp_, you’ll need to change it in the first line of the replacement code to reflect the correct database naming scheme.

    You might also check the postmeta table of your tags blog. (In my case it’s wp_28_postmeta – replace ’28’ with your tags blog id.) Check to see if you can find anything with the meta_key ‘siteurl’ or ‘blogname’.

    I just did some testing on my own site and the plugin seems to be working fine (though it wasn’t for a while, because the plugin had been upgraded to the most recent version without the modifications having been applied). On that note – and this is a silly point – you did figure out that the code to be replaced is no longer in the vicinity of 179 in the plugin, right? Now it’s around 250.

    Let me know what you find.

    Reply

Leave a Reply

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