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.

You may also like...

5 Responses

  1. Japh says:

    I’m glad to hear that people other than just the asker are benefited by solutions posted on WPQuestions. I’m also very flattered that you appreciated the solution I posted!

    As you mentioned, all 3 solutions (the 2 on the site, and your own) will do the job.

    Thanks again, and I look forward to reading your solutions to future WPQuestions :)

  2. Chris Hajer says:

    I like watching the questions to sort of “play along at home” to see if I can come up with a solution, or at least see the thought process that arrives at the solution. I looked into this a little bit too and pinged @darrylhoyt with a couple comments as I looked more deeply into it.

    I did find this from Ryan Boren from couple years ago:

    “link_updated is not used in that way. It is used when the site the link points to is updated. See wp-admin/update-links.php.”

    http://core.trac.wordpress.org/ticket/3738#comment:4

    Which landed me here:
    http://core.trac.wordpress.org/browser/tags/2.9.1/wp-admin/update-links.php

    At that point, I started thinking about WHERE to store that updated information, since the field in the $wpdb->links table was not intended for that usage.

    Then I checked this AM and saw one core hack suggestion, and cringed, and then the other plugin based suggestion, which looked good. Then I saw your post and was happy to find out I’m not the only one watching wpquestions like that :-)

    Cheers

  3. Hafiz Rahman says:

    @japh: the nice thing about your solution is that it teaches me (and I’m sure others as well) how to hack WordPress smarter.

    @Chris: Yeah, so we’ve been working on the same thing it seems. I had that similar experience: I read that old trac ticket, then some forum posting that lead to that plugin and so on. Amazing how a seemingly simple question can lead to quite an information “hunt”. All in all it’s been an educating experience for me. Cheers!

  1. January 9, 2010

    […] write such code. It was humbling to work with him and see the elegance of the code that he wrote. So I can related to what Hafiz Rahman is saying here: It all began with Darren Hoyt’s tweet: interesting new question over at WPQ courtesy of […]

  2. March 1, 2010

    […] write such code. It was humbling to work with him and see the elegance of the code that he wrote. So I can related to what Hafiz Rahman is saying here: It all began with Darren Hoyt’s tweet: interesting new question over at WPQ courtesy of […]

css.php