Displaying recent posts and comments

Displaying information about how many users and blogs you have is useful, but it would make life much easier for your visitors if they could quickly and easily navigate to those blogs.

There are a few ways you can do this. One way is to offer a list of all users' blogs in the sidebar of the main site. There is a widget called List Blogs Widget, available on the WordPress MU site, which will do this for you. This may be a good idea if you are planning on running a small WordPress MU site that will have only a few users—perhaps a site for your local parish, or even a school blog network where teachers could post updates for parents.

However, in the case of SlayerCafe.com, we are expecting a large number of blog registrations from Slayers, Watchers, and vampire hunters all over the world. So, we need another way to offer content to our visitors.

Time for action - displaying the most active blogs

Rather than displaying all the blogs on the site, we will display the most recently updated ones using a plugin called AHP Sitewide Recent Posts, available at

http://wpmudev.org/project/AHP-Sitewide-Recent-Posts-for-WPMU.

1. Download the plugin and extract it to your /wp-content/plugins folder. You may want to rename it to something more recognizable than the default name—for example, recent_posts.php.

2. Upload the plugin to your web server and enable it in your admin panel.

3. Open the r_sidebar.php file for the main site's theme.

4. Add the following code where you want the recent posts to appear. In our case we replaced the list of available themes with it.

<li><?php ahp_recent_posts(5, 30); ?></li>

5. Upload the changes. You should see a list of recent posts in the sidebar, as shown in the following screenshot:

What just happened?

We installed the AHP Sitewide Recent Posts plugin and set it up using the default options. With those settings, the plugin shows the most recent posts (excluding the default Hello World posts made when a blog is created), along with the gravatar of the poster if they have one, and a link to the list of comments made on the posts (if there are any). The plugin also shows a very short excerpt from the post.

These settings are fine for most of the cases. However, you may want to change some of them—may be removing the excerpt or not showing the gravatar.

Customizing AHP Sitewide Recent Posts plugin

The recent posts plugin does not have an admin menu. You customize the menu by editing the plugin file directly. Most of the options are set by changing the optmask variable at the start of the plugin's code.

I'm quite happy with the appearance of the recent posts list on SlayerCafe.com, except for one thing. The first five words at the start of each blog post are displayed with all letters in upper case: I THINK THAT THIS LOOKS rather strange, so I think it should be turned off.

Time for action - tweaking the recent post display options

Follow the steps mentioned next to customize the recent post display options:

1. Open the PHP file containing the recent posts plugin code.

2. Scroll down until you find the line that has the ahp_recent_posts function.

3. Find the part that says $optmask = 255 and change the number to 127.

4. Save the file and reupload it.

5. Refresh the page. The posts should now display in the way intended by their author—no uppercase characters unless the author wanted them there.

What just happened?

We changed the setup of the AHP Sitewide Recent Posts plugin by setting the value of a bit mask. This isn't the first time we've worked with one, although it is the first time we've needed to use one for WordPress MU itself.

You may remember in Chapter 2 that we had to set some file attributes—the values we were setting there were also a form of bitmask.

With the AHP Sitewide Recent Posts plugin, the options are set using a variable called $optmask. It defaults to 255, which means that all the options are turned on. The available options are listed in the following table:

Bitmask

Display option

1

Show Gravatars

2

Show Post Date

4

Show Author Name

8

Show Comment Count

16

Show Blog Name

32

Show Post Name

64

Show Post Excerpt

128

Capitalize beginning of excerpt

To calculate the value to use in optmask, you can either start from zero and add up all the options you want switched on, or start from 255 and deduct the values you want to turn off (whichever is easier). For example, if you don't want to use gravatars, but want everything else left on, your optmask would be 254 (255-1).

There are some other options in the plugin file, which you can edit directly—for example if the gravatars are too small, change the grav_size variable. There are also variables to set the author prefix, the separator displayed before comments and posts, and the way the date is displayed. A list of options is shown in the following table:

Variable

Default setting

$blog prefix

$post prefix

" | "

$auth prefix

' by '

$com_prefix

' 1 '

$date format

'D M jS, Y'

$grav_size

24

$debug

0

Pop quiz - doing the thing

b) A mask worn by very small people.

c) Something used to hide bits and bytes on your hard drive.

d) Used to set options by adding or subtracting from a pattern of characters. Answer: d

Displaying Sitewide recent comments plugin

Sometimes the most interesting articles aren't the ones that have been posted most recently, so why not give our readers another view of the content on our site? Displaying recent comments is a good way to do this. Presumably, if someone feels that a post is worth commenting on, it must contain something interesting.

We will use the Sitewide recent comments plugin to enable this feature. You can download it from http://wpmudev.org/project/Sitewide-recent-comments.

Time for action - Sitewide recent comments

1. Download the plugin and extract it.

2. Upload the sitewide_comments.php file to your /wp-content/plugins folder and activate it in your WordPress MU admin panel.

3. Log in to phpMyAdmin, select your WordPress MU database, and select SQL.

4. Paste the contents of the file CREATING-sitewide_table.txt into the text box and click ok.

¡J Server: localhost ► jpi Database: slayerblogs a Structure s^SQL -Search . Query * Export Import ^Operations

Run SQL query/queries on database slayerblogs: ©

Table structure for table " sitewide corrrrents

CREATE TABLE ' sitewide_corrrr.ents' [

" 3ite_cid" bigint (20} HOT MULL aut o_iTierement, I• I *.*) I : Digillt (20) NOT NULL, "corrrr.ent_ID" bigint (20) unsigned NOT NULL, " corrrr.ent_post_lD' int (11) NOT NULL default '0', ' corrrr.ent_author ' tinytext NOT NULL,

" corrrr.ent_author_en.ail" "varchar (100) NOT NULL default " corrrr.ent_author_url' varchar (200) NOT NULL default 1 1 , ' corrrr.ent_author_IE varcliar (100) NOT NULL default "corrrr.ent_date" datetirr.e NOT NULL default '0000-00-00 00:00:00', " corrrr.ent_date_grr.t' datetirr.e NOT NULL default '0000-00-00 00:00:00'

[Delimiter ; ] F Showthis query here again

5. Once you have created this table, log in to your admin panel and enable the Sitewide recent comments plugin.

6. Open r_sidebar.php file for the theme that you want the plugin to apply to. In our case, we will be editing both the theme on the main site and the copy used on the individual user's blog.

7. At the point you would like to have a list of comments displayed, insert

<h2>Recent Comments</h2>, followed by the code from SAMPLE_CODE.php.

The code in SAMPLE_CODE.php needs a minor alteration, though. In the code, just after the bit that says normal recent comment instead...}, just add ?>, or the desired screen may not be displayed.

On SlayerCafe.com, we added our Recent Comments code after the closing the </ul> tag that appears below the Site Stats code.

8. Find the comment that says only for blogs 1 and 2, and change the numbers in the preceding section of code to reflect the number of blogs you would like included. We would like to include comments on all blogs, so set the maximum number to 100.

9. A few lines below this, change the number after LIMIT to 10.

10. Remove the following lines of code:

$ach_blog_path = trim($ach_blog_path, "/"); $ach_blog_path .= "'s page ";

11. Replace those lines with the following:

$bname = get_blog_details($ach_blog_id)->blogname;

$ach_blog_path = $bname; $ach_blog_path = trim($ach_blog_path, ".");

12. Upload the changes. Make some comments on your test blogs and approve them in the admin panel.

13. View your blog. You should see a Recent Comments section that looks something like the following:

Recent Comments

SnQwCrO\/\/ | 03/01/2009 | Fighting Vampirism Around The WorldQh my, I'm flattered. You really need to hit the books though, kid. It's nice to know that people notice when you arrive in town, but really - you don't know who you're dealing with? I'm hurt! more

ángelus | 03/01/2009 | Defeating the Vampiric Scourgelt is nice to see you on here. I think you could teach a few of the young slayers on here a thing or two! more

What just happened?

The Sitewide Recent Comments plugin creates a new table in your database, where it stores copies of all comments made after the plugin was installed. The plugin author chose to use this method because it would mean that the plugin would use fewer system resources during the day-to-day operation of the site.

WordPress MU creates a new set of tables for each blog that is hosted on a particular install. Ordinarily, to get a list of all comments on a site, you would have to query each blog's comment table. This would add a huge overhead to the site when you consider that the query is run every time a page with the recent comments code included is viewed, and there could be hundreds of blogs on the site.

When you are using this plugin, comments are copied into the plugin's comments table as they are created, which means one extra database action every time a comment is made. However, the saving is that the plugin has to call only one table to get a list of all the comments on the site.

We activated the plugin sitewide so that it would keep track of comments on all our users' blogs.

What about existing comments?

The plugin only adds new comments to its database. If you already have some comments on your blog, and you would like to see them copied over, you will need to add them to the database manually. To do this, use the MySQL UNION statement (http://dev.mysql.eom/doc/refman/5.0/en/union. html) to get a list of all the comments in the blog, export the results in a suitable format such as a CSV file, and then import them into the new table.

The plugin display code

We made a few edits to the plugin's display code.

First, we changed the number of blogs to display comments from the default (blogs 1 and 2) to cover the first 99 blogs. At the moment, we don't have that many blogs on our site, but the code will still run and process all the blogs that it finds. We could initially set the number of blogs to 9999 to make sure that all users' blogs will be included. The LIMIT is the number of records returned by the query. We changed this value to 10—the maximum number of recent comments we want to display at a time.

The sample code that comes with the plugin is intended for sites where the users' blogs are hosted in subdirectories such as http://www.slayercafe.com/victory. As our site is set up so that the blogs are in subdomains (victory.slayercafe.com), the display code incorrectly states that all comments came from MainBlog.

We fixed this by removing the code that looks at the blog's URL to determine its name and by replacing it with a call to get_blog_details—a function found in wp-functions. php. We sent the ID of the blog to the function and asked it to return the name of the blog.

Does your blog use subdirectories?

If your blog uses subdirectories, you can use the sample code that comes with the plugin to show the short blog name. However, our edited version of the code will still work.

The get_blog_details function can be used to find out a lot about a blog from its ID. Check the WordPress Codex (http://codex.wordpress.org) for details on what the function can tell you.

Pop quiz - multiuser plugins

1. The mu-plugins folder:

a) Contains all plugins used by your WordPress MU site.

b) Is the folder used to store plugins written by the WordPress MU developers themselves.

c) Is the folder used to store plugins you would like to have automatically activated on user blogs.

d) Is no longer used in current versions of WordPress MU. Answer: c

Time for action - our improved home page

We are now displaying a lot more information on our home page, but it may not all be in the format you like. Take a look at the plugins and see if you can tweak the display to how you like it. Also, look at the Most Active Blogs plugin available at http://wpmudev.org/ project/most-active-blogs. Depending on the nature of your community, you may prefer that plugin to the recent posts that we are using.

Finally, look at the rest of the home page. In the sidebar, SlayerCafe.com still has some links to WordPress.com and some other sites. It's about time we changed these links to something more appropriate! Fortunately, this is easy to do through the admin panel, similar to the regular WordPress.

Was this article helpful?

0 0

Post a comment