I take blogging pretty seriously. When I write a post it has a very specific purpose and tends to be pretty long. I think this strategy has a lot to do with my success; but it has also raised the stakes on blogging to the point where I no longer have a container for a few hundred words I’d like to share. By container I mean a place to publish content longer and more permanent than a Facebook post but less high stakes than a blog post.
With that in mind, I decided that I needed a microblog. For a while, I thought that it could be a separate blog, maybe on WordPress.com (more on this below); but the more I thought about it, the more I realized that I wanted it to be on my personal website. So, I came up with the idea of adding a microblog to my site using the “aside” post format.
Writing every day is important; however, writing 1500 or more words each day on a very specific topic can be challenging for even the most seasoned bloggers.
In this post I’m going to explain how to create a microblog within a blog, using a post format. I chose the “aside” format; but you could use “image” post format to create a picture microblog, or the “quote” format to create a miniature quotes blog.
I created my microblog, at least in part, to take a small dip into learning more about custom rewrite rules. Custom rewrite rules are not easy to learn—so a microblog enabled me to tackle a small project that involved fewer rewrites. I’d like to thank Brady Vercher of Blazer Six for helping me figure out the rewrite rules I needed for this when I asked for help in the Post Status Slack.
In Defense of Post Formats
Post formats are not the most popular feature in WordPress, but I’m a big fan of them anyway. If you want to read my full argument in support of them, you should check out the first part of my five-part series for Tuts+ on post formats.
For something like this, I didn’t want something that stands completely on its own like a post format does. Instead, I wanted it to work just like posts; and I also wanted it to be easy to switch between a “full post” and a “microblog post,” just in case something I thought was going to be short ended up being longer. In addition, specific post formats can easily be styled differently than other formats, thanks to special body and post classes.
Other Ways to Do This
The way I solved this problem is far from the only way, and was partially motivated by the desire to figure out how to do it. There are plenty of other ways to do create a microblog that are worth considering. I weighed a bunch of different options including, and come up with some alternatives:
I considered using a custom post type, but that seemed like an overkill—not to mention it’s less portable. If I turn off the plugin I ended up writing for this, all of the “microblog” posts go back to being viewed like regular posts, since that is technically what they are. If I turned off a plugin that added the custom post type, the posts are no longer viewable. Yes, the posts are in the database, but they are no longer readable. The other advantage of using a post format was that the post editor has exactly the same features, which isn’t always the case with custom post types.
Another approach I considered was using a separate blog all together, but I really didn’t want to set up and maintain another site. I contemplated using WordPress.com or Tumblr, but in the end, I decided to keep it all in the same domain, with the same visual look, and with the ability to easily navigate to my other content. You might want something that looks totally different and has its own domain.
Making It So
Part of why I chose the approach I did was for the sake of data portability. For this reason, I created a very small plugin, which keeps me from losing these changes when I switch themes. You could add the code I am showing you below to your theme’s functions.php but I don’t recommend it. Doing so is short-sighted because it ties functionality to the presentation layer of your site (the theme) and complicates debugging.
If the code that I’m about to show you doesn’t work as expected, then the first debugging step would be to switch themes. Some themes add custom rewrite rules that might interfere with this. The best way to debug is to switch to the default theme—something that is pointless if you put this code in your functions.php.
Adding the Post Format Support
The theme I’m currently using only supports a few post formats, not including the one I needed. This can easily be fixed, however, with a function hooked to “after_theme_setup” that uses add_theme_support(). The tricky thing is that add_theme_support() overwrites the existing settings for the feature being added.
This means that add_theme_support( ‘post-formats’, array( ‘aside’ ); would remove the existing post formats. That’s why I hooked in late and got the array of currently supported formats, using get_theme_support( ‘post-formats’ ). I then added “aside” to it and redeclared post-format support. Here is how that works:
/** * Make sure theme supports aside post format */ add_action( 'after_setup_theme', function() { $formats = get_theme_support( 'post-formats' ); $formats = $formats[0]; if ( ! is_array( $formats ) ) { $formats = array( 'aside'); }elseif( ! in_array( 'aside', $formats ) ) { $formats[] = 'aside'; } add_theme_support( 'post-formats', $formats ); }, 50 );
Keep in mind that get_theme_support( ‘post-formats’) will return false if the formats aren’t supported, which is why I have the extra conditional in there.
Creating a Custom URL for the Microblog
Post formats are technically a taxonomy. That means, like any other taxonomy, they have built in archives. That said, I wanted a custom URL structure for the microblog.
This is a two-step process. The first is to add rewrite rules to add a custom URL for the microblog. The second part is to modify the term link so that get_term_link() will return the new URL.
To make the custom URL structure I want, which for me is “/small-things,” there are two rewrite rules required: The first is for paginated requests, and the second is for the first page of results. Both can be done in a function hooked to “init.”
The WordPress rewrite API is a huge topic that I can’t possibly cover in a paragraph or two. If you want to dive into this subject, the best place to start is with this excellent tutorial by Pippin Williamson.
The rewrites API translates human-friendly URLs into the actual query that WordPress uses to build a WP_Query object containing the right posts that a theme can render. When you add a rewrite rule, you describe a URL, and then tell WordPress where to map it in the application. It’s almost always mapped to index.php with a few additional GET variables attached that will be used to create the right WP_Query.
Our two rules are simple: The first adds a rule for when the URL starts with “/small-things/page/,” followed by a number. When this happens we set the query arguments for the aside post format and for pagination using whatever number is in the last URL segment as the page number. The second rule is even simpler; it works when there is no “page” in the URL, and just maps “/small-things/” to the first page of results for the post format “aside.”
/** * Add rewrite rules for small-things microblog */ add_action( 'init', function (){ //pagination rule add_rewrite_rule( 'small-things/page/?([0-9]{1,})/?$', 'index.php?post_format=aside&paged=$matches[1]', 'top' ); //first page rule add_rewrite_rule( 'small-things/?$', 'index.php?post_format=aside', 'top' ); });
For both of these rules, to change the base URL, substitute what you want for “small-things.” If you are using a different post format, substitute that for “aside.”
It’s important to note that this will not work until you flush your permalinks. For performance reasons, WordPress saves the active rewrite rules in the database, instead of rebuilding them on every page load. Be sure to go to your permalink settings page and click save to trigger a rebuild before testing this.
Now that this is in place, the new URL rules will work, but any links, front-end or back-end, to the aside post format will be affected. We can solve this by using the “term_link” filter to change the URL for the aside post format:
/** * Customize and shorten the asides term link. */ add_filter( 'term_link', function( $termlink, $term, $taxonomy ) { if ( 'post_format' == $taxonomy && 'Aside' == $term->name ) { $termlink = home_url( 'small-things' ); } return $termlink; }, 10, 3 );
Removing the Microblog Posts from the Main Index
Part of what I wanted was to avoid having these posts show up in the main blog index of my site; instead, I wanted them to have their own home. Everything I have shown so far was to provide a new place for those posts to be shown, but it didn’t affect the main blog index.
The next, and possibly the final step, is to remove those posts from the main blog index. This can be done at “pre_get_posts,” the action that runs when the arguments for WP_Query are built, but before they are used to build a WP_Query object.
We need to add a tax_query to exclude posts with the aside post format, when on the main query. There is additional logic that you can see below to ensure that we do not affect queries for the aside format. Again, if you’re using a different post format, be sure to substitute it in your code.
/** * Remove asides from main blog index */ add_action( 'pre_get_posts', function( $query ) { if ( $query->is_home() && $query->is_main_query() && ( ! isset( $query->query_vars[ 'post_format' ] ) || ( isset( $query->query_vars[ 'post_format' ] ) ) && 'post-format-aside' != $query->query_vars[ 'post_format' ] ) ) { $query->set( 'tax_query', array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-aside' ), 'operator' => 'NOT IN', ) ) ); } });
Taking It Further
That’s what I did to create a microblog on my site. You can see the full code for the plugin I wrote—all 70 some lines of it—in this gist. There are, however, a few more things to consider doing.
You may want to add some conditional logic to your theme’s “archive.php” or whatever file is rendering the archive for taxonomies according to the template hierarchy. In that file, you can use has_post_format( ‘aside’ ) to add conditional markup for your microblog.
Also, as I stated above, you could create CSS rules to target the aside post format and make it stand out more. Matt Mullenweg’s site uses a custom child theme of Twenty Thirteen that makes excellent use of this technique to highlight different formats. It works well for him, as he tends to post mainly short asides, as well as links.
Enjoy your Microblog
I haven’t been writing enough recently. Not having the right “container” for small posts is part of the problem. Now I have one less excuse not to write. I also had fun figuring out how to make this work, and in the process found something new to write about.
The funny thing is that, because of the work I do, I tend to look at WordPress as a customizable application framework and eCommerce platform. Sometimes I forget that it is also a fun and flexible blogging tool as well. I’m happy I found a fun new twist on how to blog with it and hope you will take this idea and use it or customize it to fit your own needs.
4 Comments