wordpress hackery for the day – automatically generate category rss links for category archive pages

Thursday, January 29th, 2009

i’m helping my dad set up a blog for his law office, and he’s decided to focus on a few main category areas and build up audiences for each.

i told him that we could use the category archives that wordpress generates to filter the posts. then we could link to these pages and style them however we want as we design the site as a whole, and people can subscribe to the rss feeds for only the categories they want.

i was not lying, but today when i actually set about to ask the category feeds to play nice with my dad’s theme, it was a little bit harder than it should have been, and i decided to blog about my solution.

the theme has a big friendly “subcribe to our feed!” link, that i like very much, and it uses a file that it calls ‘subscribe.php’ to generate the link, which i also like. for those of you playing along at home, that means that you can just include ‘subscribe.php’ anywhere you want an rss link to show up, and if you ever want to change the way the link works, you only have to update one file. hurray!

for my purposes, however, a different rss link needs to be generated when you are on a category page, and it has to be the right one. one way of doing this would be to make a custom category template for each page, which wordpress lets you do by making pages like ‘category-17.php’ in your theme folder. but that would be annoying, and it would take away the benefit of having only one piece of code to change, which would be even more annoying, not to mention bad practice.

the better way is to add a condition to the ‘subscribe.php’ file that checks to see if the page you are on is a category archive, and then generates the appropriate rss link. that should be easy enough, but i couldn’t find a function that automatically retrieves category-specific rss feeds, and the otherwise convenient fact that there are several different ways to refer to rss feeds (but only one way to refer to a category-specific feed), was giving me trouble.


i spent a while searching the wordpress codex to see if someone had already written the code or a plugin for me, because it seems like the kind of thing that would be a relatively common desire. sure enough, i found lots of people who wanted to do this, but only one who seemed to have succeeded, and his solution seemed really wonky to me and i wasn’t sure it would even work in the new version of wordpress.

in many ways, the strength of the wordpress community spoils me for times like this. i can almost always find answers to my codex questions in less than five minutes, so when i can’t, i just assume the answer isn’t out there, which probably isn’t true. in any case, if i can’t find the answer before i get tired of looking, other people probably can’t either, so it seemed like a good opportunity to increase the number of answers in the pool. :)

after a few frustrations with brackets and such that are par for the course, i made it work!

‘subscribe.php’ now uses the following code to generate the RSS link:

" title="Subscribe to this Feed via RSS">Subscribe via RSS

that’s a lot of babbling so i won’t say more unless there are questions.
good night and good luck!

geek out a little more, please

Tuesday, June 5th, 2007

over the weekend i took some much needed time to start figuring out how to wrap my mind around the question of what next, and in order to do so i sat down with a piece of paper and started making lists. what are my priorities right now? what regular expenses do i know that i have for at least the near future? what potential sources of income do i have that satisfy the latter while still furthering the former? what steps do i need to take to make those sources viable?

this was a very good exercise. i ended up with a couple interesting ideas and have some tangible things to work on, so i’ll talk more about how those things unfold as they move along. right now, though, i want to take a minute to talk about making lists, because it really is one of the most powerful activities i know, and for some reason i realize this a few times a year and yet still never discipline myself to just sit down and do it with any consistency or regularity.

i know it’s obvious but lately i’ve been really struck by the idea that all these things i know would make my life better but still don’t do? like exercising more or eating better or not letting the dishes pile up or drinking more water? it’s not laziness that keeps me from biting the bullet. it’s guilt. for some reason i think that, if i was really built for such endeavors, i would be doing them already. and maybe one day i will just wake up and figure out what’s holding me up and never look back. like it’s a matter of triggering some secret internal switch, rather than admitting that the only switch to flip is the one that makes me devote energy to looking for switches when the lights are already on.

anyway, when it comes to listmaking, a big hurdle for me is that i have grandiose visions. you see, i happen to believe that computers are enabling an evolutionary shift in our ability to classify and manipulate information. i believe that we are on the cusp of being able to think Better, and figuring out what that means and how i can help it along is the reason i meddle in these things we call HCI and design. i know in my gut that listmaking is a part of the picture, and so i find it very difficult to sit down in front of a piece of paper or a computer screen without coming up with a host of ideas that get me really excited but which i have neither the time nor the ability to implement before dinner, which frustrates the meddlesome intermediary step of writing down what i need at the grocery store.

this is incredibly lame, since i will never get to the point where i can build the tools i want until i get over myself and manage my ideas with the tools i have, so i just need to shut up and knuckle down, which is what i told myself this weekend. then the topic came up yesterday as well, when i sat down with the lists i had made and tried to decide where to take them next, and it was in the midst of this thinking, while on a blogroll break, that i tripped over something i have encountered in the past but never given much thought, and that is the world of text-only to-do management.

the to-do market in web 2.0 land is pretty intense. the good folks over at 37 signals offer tadalist, which suffers a bit because in some ways it’s just an attempt to get you to spring for their not-free content management apps, and it kinda shows. more excitingly, there are remember the milk and gubb, both of which have their devotees and each of which has different strengths and weaknesses. and some folks just hack their way through the to-do jungle using various elements of the googlesphere.

but nobody gets it Just Right for goldilocks, so she keeps trying out new chairs…

meanwhile, back in web 1.9, some geeks staunchly defend the practice of keeping their to-do list in a .txt file, and pundits scratch their heads, because how can you have gradients and rounded corners with a freakin’ .txt file?! avert thine eyes!

said pundits sometimes cough uncomfortably, however, when it is mentioned that one person who is not above this approach is marissa mayer, director of consumer products at google, which always makes its way into interviews somewhere. apparently she has sections for each person she deals with and, i don’t know, different projects and stuff, and she sits down every day and figures out what to do based on some sort of magical system that is never really discussed directly. kind of like pagerank! :)

this factoid has always made me go “huh…” without really inspiring me to jump on board. i mean, i can See the advantages of a digital dumping ground to a paper one because i could theoRetically cut and paste and not have to rip out scribble-filled pages, but… that is a world where theory and practice will never meet, folks. it and the world where i could theoRetically go on a 5-mile run before breakfast every day can meet up for martinis and trade wisecracks, and i’ll just keep living here in the land of things i actually care to focus energy upon.

anyway, what i realized today (with more “duh” than “huh”) is that i have been a bit dense. my inner computer geek has been making up for a lot of lost time over the past few years, and i predict that it has still not reached its mature state, but i still sometimes fall prey to the fallacy of forgetting that my power to sic the computer on a given chunk of information is independent from the existence of buttons and dropdown menus. and in this case that means that i forgot just how much one can do with text if one is not afeared of the command line…

so yeah, geek alert #FFFF00 (yellow), k?

gina trapani at lifehacker, who spearheads a little community called todo.txt, builds her system around a plain ‘ol .txt file full of lines like this:

p:blog @home @computer @offline write about list management

where p:____ denotes a project and the “@____”s denote the contexts in which the task might come up, an approach that is in line with the rhetoric of productivity guru david allen, who founded and leads the cult of GTD (Getting Things Done).

GTDisms aside for the moment, you can see the basic idea of a list with tasks and categories, and with a bit of grit this list can be updated directly from the command line like this:

echo '@store @grocery @cooking lemons' >> todo.txt

and knowledge can be intelligently extracted from said list like this:

grep @grocery todo.txt

which spits back the whole line “@store @grocery @cooking lemons”, along with any other lines that have @grocery in them. a bit wonky, but i am nevertheless empowered to fight scurvy with deliciousness, so i might best think twice before turning up my nose at such an offering.

and presto!
you slice it, you dice it, you email it to yourself, you add categories on the fly, and that mess of a .txt file starts to feel “interactive” pretty quick.

there is actually a pre-written script package at todo.txt that makes it so you can just say stuff like:

add @store milk
list @store

and also archive and complete items without getting too wacky.

so that’s handy, and with a few basic scripting skills of your own, you are well on the way towards the sort of testing and tweaking that i haven’t been able to find the patience for when it comes to paper or learning to play nice with someone else’s API.

and all while giving the inner geek a little playtime and not relying on someone’s flashy website that might go down because cats are in ur serverz.

i expect that david, as my personal grep evangelist and “what you UI people always seem to forget is that building top-notch UI’s takes energy” foil, will be absolutely ecstatic over this post once he returns from his current mission to decrease international ignorance of the glory of bryce and zion, and to all this i say, work well done, /_\.

but it’s all in the name of expending that energy more efficiently later.
or something…

i know this is all rather rambly, but i think what i’m saying is that i realized, in thinking about all of this, that it is important to grant that it is possible to overlook the experimental value of old skool hackery in the pursuit of the user experience. we design heads get in this place where, just because we wouldn’t be caught dead releasing something into the wild, we think there is nothing to learn from it. in our hurry to put things before the user early and often, we limit the scope of the questions we are able to ask, and in doing this we are in danger of distancing ourselves too much from the creative potential of a good raw geekout.

this is touchy, because i am not at all saying that we should back down from the cause of user-centered design, nor am i saying that all designers need to be computer geeks. i’m just saying that we should remember that the point is to expand the computing family tree, and we don’t do that by severing the roots, or by telling the people who want to climb up a mountain the hard way that they are silly for not taking the new paved trail.

every trail has a different view, after all, and sometimes we have goals other than just getting to the top as quickly as possible.

ok, too much metaphorical meandering.

i’ll read this later and see if it makes any damn sense, and in the meantime, i’ll take a crack at organizing my life from the command line, so that you don’t have to.

doing my part

Saturday, May 26th, 2007

i installed a captcha plugin on my blog.

this means that when you want to comment, you get to play one of those little games where you type in letters to prove that you are not a robot (sorry victor ;).

i have been thinking of doing this for a while, but never got around to it, until today when i was pushed over the edge by the discovery of the cleverest distributed computing idea since seti@home. and maybe even cleverer. cuz aliens? they might already be here kicking back with some watermelon while they wait for us to wise up a bit. did you see my pictures from last weekend?

the captcha program i found, however, is all about the improvement of our earthly existence. it is called recaptcha, and it works by fusing two pieces of tedious data entry – captcha verification and OCR proofreading – into one handy package. in short, every time you comment on my site, you are helping to create a more complete digital library for us all. as the recaptcha site explains:

About 60 million CAPTCHAs are solved by humans around the world every day. In each case, roughly ten seconds of human time are being spent. Individually, that’s not a lot of time, but in aggregate these little puzzles consume more than 150,000 hours of work each day. What if we could make positive use of this human effort? reCAPTCHA does exactly that by channeling the effort spent solving CAPTCHAs online into “reading” books.

this is so unbelievably clever that i cannot get over it. and since it took about ten minutes to sign up and pimp my website with the already built wordpress plugin that handles all the computer talky talk, well… let’s just say i can already check “further the revolution in some small way before lunch” off my todo list for the day.

and that just makes a body feel good, ya know?

thanks, as often, to lifehacker for the heads up.

now back to saturday blogroll catchup. fun, ain’t it?


Wednesday, May 16th, 2007

something i’ve been wanting to do for a while is shift from the boring and overwhelming list of categories on my sidebar to a tag cloud in the ultrahip web 2.0 folksonomic tradition. this is not because i aspire to be hip like three snaps in a 2(.0) formation, but because i think clouds are a good idea.

i like clouds because:
they have more information in them than lists (size is information!).
they are prettier than lists.
they take up less room than lists.
they are where the care bears live.

what’s not to love?

once upon a time, in a spurt of energy to this end, i downloaded and installed the formidably named Ultimate Tag Warrior plugin, which promises nifty tag cloud goodness for all, but which ended up requiring too much of me in order to realize such goodness, so i let it drop.

i think i was frustrated because i knew deep down that it was not a very complicated problem, but it was still too much for me to think about on any one given day, so i ignored it for a silly long time.

therein languish so many of the ills of this world…

but today i prevailed!
go look! (you have to be on the main page, not viewing just this post)

and if you don’t want to hear anything geekier than that, you should stop reading, because now i’m gonna talk about code and stuff. so consider yourself warned.

i decided to search around again for options and advice, and i found very fruitful beginnings in:
this blog entry wherein the code professor churns out some wordpress-ready php for cloud-building, he being inspired in turn by
the technology evangelist writing about using php and css to build tag clouds for movable type blogs.

i liked the approach suggested by them because the core idea for wordpress is simple:
1) replace the part of the sidebar.php file that goes to fetch the category list with your own code that makes a tag cloud.
2) size appropriately using css.

once i thought about this, installing a plugin for the task felt silly and much less fun, so i was inspired to follow their trail.

the php as provided went through the trouble of counting the number of posts in each category even though wordpress provides a function for that, but then someone in the comments pointed that out and offered some new code, so that was nice.

the css was taken from the technology evangelist as written, and i didn’t really like it. it basically assigned each tag an id based on the number of posts in the category and then sized the tags based on that number, which is reasonable in theory except in practice it led to css brackets like this:

a.tag4 ,a.tag5, a.tag6 {

for each triad of numbers up to 30.

and apparently it just expects that no category will have more than 30 posts.

this seems short-sighted to me, particularly because if you were a somewhat balanced category user and you blogged for a number of years you would eventually get to the point where almost all of your tags were the same size. which defeats the purpose.
and sure you could go in as you wrote more and change the numbering scheme, but who wants to do that?

it makes more sense to me to size each category based on the percentage of total posts. in the beginning all your tags would be big because each post would be a large percentage of the total, but it seems like it would equalize out pretty quick, and then scale well with time.

the only downside is that it requires you to count all the posts, and writing another loop for that seemed a little excessive. i did a little more searching and found this, which offers a php function that will get the total number of posts for you. apparently people want to do that somewhat often, but wordpress doesn’t support it directly. yet. but i still love them.

anyway, here is the code that i ended up with after mashing those bits and pieces together and fiddling with dangling quotation marks and asking david for some help because he conveniently appeared on IM right when i was having an issue.

thanks, david!

if you are running wordpress and want a tag cloud like mine, open your sidebar.php file (it’s in wp-content/themes/youractivetheme), find the part that looks like this:

and replace it with this:

categories ORDER BY cat_name";

$cats = $wpdb->get_results($qrystr);

//get total number of posts to use for normalizing 
//the size of the category tags
$totalposts = $wpdb->get_var('select count(*) from wp_posts 
where post_status = "publish"');

//read and write the category data
echo '
'; foreach ($cats as $cat) { $catname = $cat->cat_name; $catlink = get_category_link($cat->cat_ID); $postcnt = $cat->category_count; $catweight = $postcnt/$totalposts; /*this part sets the values that are used to do the tag sizing. you can fiddle with the percentage thresholds if you don't like the size distribution within your cloud. if you only have a few posts or a few categories, everything will be big for a while. blog more! :) you can also change the id text if my flippancy annoys you. just be sure to change the css too. and bah, humbug. ;) */ if ($catweight == 0) $tagweight="zilch"; elseif ($catweight < .02) $tagweight = "notmuch"; elseif ($catweight < .05) $tagweight = "some"; elseif ($catweight < .1) $tagweight = "couplefew"; elseif ($catweight < .15) $tagweight = "several"; elseif ($catweight < .25) $tagweight ="lots"; else $tagweight = "damn"; echo ''; echo $catname," "; } echo '
'; ?>

then open your style.css file and add this:

#cloud {padding:1px; line-height:20px;text-align:center;}
#cloud a {text-decoration:none;padding:0px;}

a.zilch {

a.notmuch {

a.some {

a.couplefew {

a.several {

a.lots { 

a.damn {

then tell me if it works and if you like it!

now it is time for bed.

UNEXPECTED PLUG: putting lots of code in your wordpress posts without it interpreting the code as code is aNNoying. i used this plugin. it made me happy.