08/02/2017

The archive contains older posts which may no longer reflect my current views.

# Writing up a post detailing how I have implemented a microblog alongside the main feed in preparation for the launch of Micro.blog. What steps I took, the code or plugins used and why I made the decisions I did.

Preparing for the microblog

In this post I will outline why I wanted to self-host a microblog, what I felt was required to do it properly, how I accomplished the various steps and, where appropriate, explain why I made some of the decisions I did.

I'll preface the following by saying that I am not a developer by any stretch of the imagination but I know a bit of PHP and WordPress structure - not to mention how to search!

There are probably more elegant solutions to the problems I have tackled but what I have seems to work okay and I am happy with that.

Background

When I rebooted the blog back in March 2016 I wanted to keep the old posts separate and have a fresh start. All previous content needed to be available but I didn't want it immediately visible.

To achieve this all posts prior to March 2016 were added to an Archive category then removed from the main display with the Ultimate Category Excluder plugin.

As a category the archive is easily accessible but, to keep things clean, I used the Remove Category URL plugin to reduce the path from /category/archive to just /archive.

Because I wanted a very minimal appearance the blog does not have a menu so navigation was added to the footer. The above seemed a good place to begin.

Part of the reason for rebooting the blog was to get all my writing back under my own control. I may cross-post to Medium - where I encourage responses having disabled comments locally - but everything is posted to WordPress first.

While I still believe in Twitter as a force and as a product the overriding atmosphere pushed me away. Coming across the Micro.blog Kickstarter reminded me that I still wanted to post but it was just the environment that discouraged me from doing so.

Having a system for short (micro) posts that allowed for networking whilst retaining that same element of independence and control was an attractive prospect.

The goal

In preparing for the launch of Micro.blog the aim was to create an entirely separate presentation and feed that didn't interfere with, and wasn't visible within, the main blog and RSS feed.

In keeping with the existing minimal look and feel the microblog had to be presented with the minimum of fuss without having to create any new page templates.

The what

  • create the microblog
  • remove it from the main view and feed
  • create a separate, cleaner RSS feed with custom template
  • tweak the theme to better present short posts
  • set the number of posts per page for the microblog separately from the main blog
  • ensure the correct RSS feeds were presented

The how

Create the microblog At the most basic level, creating the microblog is easy: just create a new post category. WordPress automatically generates an RSS feed at http://site_URL/category/category_name/feed so that's it, we're finished, right?

Technically, yes. You could leave it at that but the microblog would be included in the normal blog view and RSS feed while its own feed contained unnecessary elements.

A bit of surgery is in order.

Separate the microblog As I wanted the microblog to be entirely self contained it needed to be removed from the main posts page and RSS feed. My initial thought was to use the category excluder as with the Archive but, having not yet decided how I was going to handle the feed, wanted another way.

The microblog had to be accessible only via its own page and RSS feed. Adding the following to functions.php did the trick to remove it from unwanted locations:

function exclude_category( &$wp_query ) {
   if( is_home() || ( is_feed() && !is_category() ) || ( is_archive() && !is_category() ))
   {
      set_query_var('category__not_in', array(156)); // Exclude category with ID 156
   }
}

add_action('pre_get_posts','exclude_category' );

You could remove the microblog category from the main feed by using a URL modifier: http://site_URL/feed?cat=-156 but I prefer keeping the user-facing side tidy.

Custom RSS feed With the microblog set up the next stage was to create a new streamlined RSS template to better suit status update style posts. I have been taking a degree of steer from Manton Reece on this who reminds us, for example, that the item title is an optional element.

The default WordPress rss2 template (feed-rss2.php) was copied from the wp-includes folder to the root of my theme ready for editing and called rss-microblog.php.

Various elements were removed or replaced to reduce the clutter added by WordPress, for example:

  • do_action( 'rss2_head' ); was replaced with manual entries to better describe the feed
  • the rss_enclosure section at the end of each item was removed which includes do_action( 'rss2_item' );
  • the <title></title> tags within the item were stripped out as they are redundant

With the custom template in place, the following was added to functions.php to create a new feed and associate it with the new template:

add_feed('microblog', 'microblogRSSFunc');

function microblogRSSFunc() {
   query_posts('cat=microblog');
   include_once(get_template_directory() . '/rss-microblog.php');
   wp_reset_query();
}

When a custom feed is created in this way its address is http://site&#95;URL/feed/feed&#95;name - the feed name is the first argument passed in the add_feed command.

As I use the JetPack sharing buttons (AKA sharedaddy) for the blog these are included as part of the item content by default so needed to be removed from the microblog feed. Adding the following line to microblogRSSFunc() takes care of this leaving a pretty clean feed.

remove_filter( 'the_content', 'sharing_display', 19 );

I found that, despite the main WordPress settings saying feeds should show 20 items, the custom feed was only giving 5. This was rectified by adding a custom query to the template:

$postCount = 20;
$posts = query_posts('showposts=' . $postCount.'&category_name=microblog');

One final touch was to completely remove the WordPress Generator tags from both the site and the feed. This was purely a vanity move and achieved by adding the below to functions.php:

function remove_wp_version_rss() {
   return'';
}
add_filter('the_generator','remove_wp_version_rss');

Editing the theme I won't go into this much as it's very specific to my setup but, as the microblog is just a category, a degree of tweaking was needed to present it in a different way to a normal category page.

Fortunately, WordPress has me covered and gives the category-category_name class to work with. By applying category specific styling such as .category-microblog p the look can be altered for only microblog posts without requiring a different page template.

The blog is configured to only show 5 posts per page via Settings > Reading which is fine for longer posts but not really suitable for the microblog. As it is a site-wide setting I have overridden it for the microblog to use 20 posts per page as below but am considering infinite scroll for this category.

function category_filter_pre_get_posts( $query ) {
   if ( $query->is_main_query() && is_category( 156 ) ) {
      $query->set( 'posts_per_page', '20' );
   }
}

add_action( 'pre_get_posts', 'category_filter_pre_get_posts' );

Tidying up

With the heavy lifting done all that remained was to ensure the correct RSS feeds were being exposed.

Because both /microblog/feed and /feed/microblog exist I redirected the former to the latter in .htaccess.

The default RSS feeds added to the header by WordPress were removed using the below in functions.php and manually replaced with the desired \<link> tags:

remove_action('wp_head','feed_links',2);
remove_action('wp_head','feed_links_extra',3);

And that's it.

There is no doubt a better and cleaner way to do half of the above but it's been good to explore and document the process.

3 comments: click to read or leave your own Comments

# New post up detailing how I set up the microblog and custom RSS feed: Preparing for the microblog.

# I'm pleased with how the "Preparing" post came out and it's good to have it all documented. Things are coming together. I've not been this excited about the blog in ages.