WPQuestions: A Humbling Experience
It all began with Darren Hoyt’s tweet:
interesting new question over at WPQ courtesy of @jophillips, any ideas? – http://bit.ly/7NOEl3
The particular question was about adding an “old” or “new” class for a listed Links based on whether the Link is inserted less/more than 31 days from now.
Curious, I dug around the Codex and into the core files. I began to think about filtering wp_list_bookmarks. Tabs upon tabs were opened, lines of code tested. Finished, I logged in into my WPQuestions account and opened the page again.
Amazingly, the question was already solved (this is only a couple hours after Darren linked to it), and there I saw two different solutions, both solved the problem with a short and easy to understand piece of code. One of it involved editing core file, which generally is undesirable, but both codes were undeniably elegant and to-the-point.
Lesson Learned
Compared to those two codes, mine was much longer: it involves regular expressions, string replacement, and so on. While it did work, I concluded that I was just thinking too far. The first solution didn’t worry about editing core files, while it’s something completely out-of-question for me. The second solution simply do away with creating its own list, while I insisted on making use of wp_list_bookmarks‘ output, forcing me to go the regex route.
Aside from that, I also struggled with a couple of curious cases: first was that wp_list_bookmarks will sometimes wrap an anchor text in an <em> tag for God knows what reason. Quite a lot of time was spent looking around for why it happened, while it’s not directly related to the problem. The second was the revelation that WordPress does not record the time when a Link is inserted, only if it’s updated. There’s actually a plugin that fixed that problem.
To put it short: I had too many rules in mind while looking for a solution.
I don’t think that is the wrong way per se, but I learned that sometimes, especially if it’s a time-critical problem, finding a faster solution can be more desirable.
So thanks WPQuestions. I’ve always thought that the site could be a good place for learning and dealing with actual WordPress use cases, and today’s experience proved that once more. If you do WordPress development, I’d really recommend going through the archives and see the various awesome ways people came up for a curious problem.
My Code
Just for fun, here’s my solution (it goes into the theme’s function.php).:
function bookmarks_with_date_check($base) { $base_arr = explode("\n", $base); $get_dates = '/Last updated: .+\)/'; $new = ""; foreach($base_arr as $value) { preg_match($get_dates, $value, $caught); if(! is_null( $caught[0] ) ) { $cut = str_replace("Last updated: ", "", $caught[0]); $cut = str_replace(")", "", $cut); $timestamp = strtotime($cut); $difference = abs(time() - $timestamp); if ($difference > 60 * 60 * 24 * 31) { // add class="old" $value = str_replace('<li>', '<li class="old">', $value); } else { // add class="new" $value = str_replace('<li>', '<li class="new">', $value); } } $new .= $value . "\n"; } echo $new; } add_filter('wp_list_bookmarks','bookmarks_with_date_check');
Note that it’s useless without the aforementioned plugin, and it requires ‘echo=0&show_updated=1‘ parameter for wp_list_bookmarks. Again, too many rules and not really elegant.