# 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_URL/feed/feed_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.
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.
I signed up for the kickstarter of micro.blog, it went live earlier this week.
Micro.blog The service is very new and so far has changed and developed every day. The idea is, you publish short posts, these are mirrored on micro.blog/yourusername via RSS. The posts can be from any RSS feed. You can get a micro.blog hosted blog at yourusername.micro.blog or use your own hosting. The micro.blog iOS app will post to your micro.blog blog or your own WordPress blog. Or you can use your own system. There is a microblog bot that will post your posts on to Twitter too. The difference between the hosted blog and your micro.blog/username stream is a mite confusing at the moment. I wonder if a different domain name might have helped. Both the hosted blog and the twitter bot are paid for options. The docs make it clear that you can host your own and point to IFTTT as an alternative to the bot. The system follows the indieweb principle of controlling your own content and sending it on to other spaces. Replies on micro.blog to your posts are sent as webmentions to your own blog and show up as comments if you have the webmention plugin installed. I had that already to get twitter replies as comments. My setup I’ve added a new category here, micro. I’ve edited the blog to not have posts with this category show on the home page, they show on micro instead. I set the micro.blog app to create posts with the status format in the micro category. I turned off the jetpack social posting to Twitter function. I’ll manually post normal posts. I’ve set up a micro.blog bot to post to Twitter. The service is very much a work in progress, and I’ve not really read the docs but I’ve noticed a few interesting things. titleless On is that the posts on micro.blog consist of descriptions with no titles. When you post form the app, you get a post on your blog without a title. A post with a title on your blog is posted as a link to micro.blog. With a post without out a title the description becomes the content of the micro.blog post. That means you get lots of posts listed in your dashboard as ‘no title’. Since I didn’t like this I tried to auto add titles to posts without titles with a little Google-fu and some WordPress coding. This worked out fine, except the posts on micro.blog consist of a title and a link, the tweet posted by the twitter bot is the same. I am now looking to create a custom RSS feed without title. More googling ahead. Alternatively I could use the code from Tweaks for micro.blog that adds dates as titles, micro.blog ignore these. Or just learn to live with ‘no title’ posts in the dashboard. Me on Micro.blog Preparing for the microblog is a lot more coherent than this post if you are looking for setup advice. I’ll post the code I’ve mentioned above at some point, it is pretty simple stuff.
Share this:
Click to share on Twitter (Opens in new window) Click to share on Google+ (Opens in new window) Click to share on Pinterest (Opens in new window)
@colinwalker thanks for the post on getting WordPress sorted for micro.blog, preparing for the microblog, very useful. I need to revisit my own tinkering. Like this: Like Loading...
This Article was mentioned on colinwalker.blog