Oct 25

Feed Comments Plugin for FeedWordPress

A lot of the background for this post and plugin come from Martha Burtis’ excellent work , but the TLDR of it is this:  We use FeedWordPress to setup course blogs that connect a mother blog to student blogs on Longwood Blogs.  However, FWP doesn’t bring in comments or comment counts from the student blogs, which is lame.  Martha found a way to hack individual themes for mother blogs with code that displayed the comment count using the comment post feeds generated by the student blogs.

This was a breakthrough but a couple things made her code a bit impractical for us:

  1. You had to fork and modify the theme code for every mother blog that you wanted this feature on
  2. The aggregation of the student comment feeds occurred on page load, so it could not only slow down the load for visitors but also could slow down the server due to redundant aggregation.  (Although, apparently WordPress has a way to reduce this aggregation performance drain automatically.)

I mulled over these problems for a while and finally decided that three things could fix them and allow us to implement comment count feeding on our own course blogs.  One was to just make the code plugin-based, so no more theme modifying.  Another was to employ a caching / cron mechanism to reduce the performance hit.  I have used caching a lot in my other plugins and recently employed crons in our new library website, so that was all pretty straightforward for me.

The crux of this plugin, though, is the piece that allows you to change how the comment counts display through a plugin.  Luckily, WP has hooks and filters for just about everything you might want to modify and that included the display of comment counts.  I just basically hooked into the filter “get_comments_number” with the following function code, which read a custom field I had saved on every post that held the aggregated comment count.

public function feed_comment_count($count, $post_id) {
	if (!strlen($comment_number = get_post_meta($post_id, "commentNumber", true))) {
		return $count;
	} else {
		return (int)$comment_number;
	}
}

You can download all 250 lines of this plugin below. I think it would also do well on wordpress.org’s library of plugins, but I’m still teasing out some timestamp / timezone issues with the options page. These don’t affect the operation of the plugin—they’re just confusing to users. WordPress forever baffles me with their choice to include their own time functions instead of relying on PHP, but that’s a story for another time.

Download feed-comments.zip

In the future, I’d also like to add a widget to this plugin that shows recent comments on syndicated blogs.

Sep 14

Adding Google Custom Search to Your WordPress Blog

For our website redesign, I wanted to do a 404 page that would be able to show Google Custom Search suggestions based on what the requested page’s URL was.  There are several plugins that do something similar to this, but they were either too convoluted or not exactly what I wanted.  So here’s what I came up with instead.  It can also take over the standard WordPress search.

What you need

This will take some code editing for whatever WordPress theme you’re using.  I’ll assume that your theme of choice is using standard file locations.  If you don’t already have jQuery loading on your theme, you’ll have to add some code to do that.  Add the following to your header.php before the wp_head() call.

wp_enqueue_script('jquery');

Overview of steps

  1. Setup Google Custom Search and Google API Console
  2. Create AJAX Request Wrapper
  3. Create jQuery JavaScript
  4. Modify 404 page
  5. Modify search page
  6. (optional) Setup Google Analytics site search

Continue reading

Sep 05

Post-Redesign Wrap-Up

New Library Website

Unfortunately, I’ve failed in my goal of writing at least one post per month.  But let’s be honest, this was going to happen eventually.  With getting the Library’s new website live,  I was under a bit of a time crunch and had no time to muse about technology.

But now that the website is complete, I’ll try to share whatever nuggets of wisdom (and code) I gleaned in the process.  There should be lots that I can share given that I was able to build the site on WordPress and used a generous amount of jQuery.  But I’ll start simple with a few suggestions for plugins and other code that I used on the project.

  • WP jQuery Lightbox — This is a relatively simple lightbox plugin for WordPress that I like because it automatically applies itself to existing images on your blog.  I also use it on my personal blog.
  • TinyMCE Advanced — This plugin lets you customize the editor for posts and pages and allows access to all of TinyMCE‘s functions.  I particularly like it for its inclusion of table editing, smileys, and font size choices.
  • Specific CSS/JS for Posts and Pages — This plugin and the next are really about having the ability to customize specific pages in WordPress.  Being able to add JavaScript and CSS to a particular page is crucial for some features like interactive elements or unique designs.
  • Custom Sidebars — Like the last plugin, this one is all about customizing specific pages.  We used to use Widget Logic for this, but it could be a bit clumsy.  With this new plugin, you just create new sidebars for use on a select page.
  • Multiple Galleries — The functionality for this plugin should really be rolled into WordPress itself considering how basic it is.  This plugin merely adds the ability to select which attached images in your posts and pages are used in a particular gallery.  This makes it possible to easily add multiple galleries in a post.
  • Visual Form Builder — There are a lot of good form editors for WordPress—there’s even one built into newer WP versions.  But this one struck the right balance between simple interface and powerful features with me.
  • Google Analytics for WordPress — Although you could go commando with Google Analytics, this handy plugin exposes even more options and data collection for you.
  • SlidesJS — A simple jQuery slider that I love to use because it’s easy to fit into any project, especially WordPress ones.
  • Tablesorter — A nifty plugin for jQuery that gives you sortable tables quick and painless.

Mar 28

Useful Web Developer Sites

Almost the end of the month…must post something.

Well, I’ve been doing a lot of website development lately, specifically good old HTML and CSS, for both the library’s website and my own site.  Naturally, I’ve had to research various CSS rules as things seem to change in this area year-to-year—I mean only just last year did people finally write off IE6 support.  The only thing that doesn’t seem to change is that IE is always the major irritant when it comes to making sure all major browsers are supported.  To that end, let me recommend the site When can I use…, which offers detailed insight into when browser market share and browser capabilities allow you to start comfortably using new CSS features.

Another website I use a lot when in the early stages of designing is Kuler from Adobe.  Pretty much the only thing it does is lets people create and share color palettes.  While I don’t always find the exact palette I want here, it does usually get me in the right direction.

Finally, another supremely nifty little site is the Ajax loading gif generator.  Its only function is to make animated GIFs that you use while JavaScript (usually with AJAX) is loading something.  This is great because GIFs only have 1-bit transparency and not 256-bit partial transparency like APNG, so they tend to get rough looking edges when using transparency with more complex loading animations.  That is unless you just apply the background color of your webpage into the GIF itself, which is what this site does for you.

A demonstration:

Black BG Trans BG

Feb 17

Free Featured Images

I’m not sure whether or not these will be useful to anyone, but here’s some graphics I did for use as featured images, i.e. for use in a slider or spotlight image.  As with other things I’ve released here, the vector art downloads are in Paint Shop Pro X2 format.

Jan 13

Deleting Sample Page in Five Lines

WordPress creates a sample page called (descriptively enough) “Sample Page” whenever you start a new blog.  However, it seems that a lot of our bloggers miss the fact that this page should either be edited or removed and this leaves an unsightly “Sample Page” page.  Bleck…nobody is going to hire anyone with that on their blog.  I think they used to call it “About” in a previous version of WordPress, which wasn’t as bad, but this “Sample Page”… it just gets under my skin (as you can tell).

The plugin New Blog Defaults surprisingly doesn’t include this option.  I even asked the author about the possibility of adding it, but she didn’t respond.  Therefore, I just did a super short plugin that removes that ghastly page once and for all!  Here’s the code:

function lu_death_to_sample_page($blog_id, $user_id) {
	switch_to_blog($blog_id);
	
	wp_delete_post(2, true); //post ID 2 should be the sample page
	
	restore_current_blog();
}
add_action('wpmu_new_blog', 'lu_death_to_sample_page', 10, 2);

This plugin will probably make it onto Longwood Blogs in the next update soon.