the official blog

Humbling experiences: when other programmers write simpler, more elegant code

Many times I’ve been humbled by other programmers. Just this last summer I worked on a project with Scott Meves. We were using the Symfony framework, the same framework that WP Questions is built in. Scott is a true genius when it comes to Symfony. I recall we needed to do some JOIN calls to the database using the Propel ORM, and Scott always knew the perfect, most concise way to 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 @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.

…To put it short: I had too many rules in mind while looking for a solution.

The question was “Change the class of a blogroll link if older than 30 days“.

As I said, I can relate to what Rahman is saying. I certainly know what it is like to spend hours thinking about a very complex solution to a problem, and then later realize (or have someone show me) that there was a more elegant solution. I hope WP Questions continues to attract such quick, elegant solutions as those offered for this question.

Responses to your responses

We are grateful for all of the praise and feedback that we have received over the last month. I’m going to take this opportunity to respond to some of it.

Thomas Maier writes::

WP support. Basically what you expected, right? Yes, from an superficial perspective. WP Questions introduced a whole new idea to a world which was totally uncommerical: Payment. And what it makes so cool is that these fucking five bucks or what you spend for a emergency question make it incredibly efficient and really fast. You’ll get quick and professional answers. And ideally by answering questions of others you “win” your money back. And above all it has a great and simple design which impressed me quite a bit.

That sums it up well: introducing a commercial element is the aim. I think the free forums are somewhat unfair to the experts who volunteer their time there. Ultimately, the success of WP Questions should be judged by whether or not we make it easier for experts to earn money off of their knowledge. In response to the launch of WageMachine, I said:

I’ve had stretches of $100 an hour work followed by stretches of almost no work coming in, and I’ve been left wondering, why isn’t there more in the middle? And why can’t my own wage vary up and down casually, depending on circumstances, including such trivial circumstances as:

Am I at a friends house, waiting on a friend?

Am I bored and trying to kill time?

At such times I often go on to forums and answer people’s questions. Such work isn’t worth $100 an hour but it is worth something. So why aren’t there more places where I could pick up some money for answering such questions?

Implicit in the idea of “make it easier for experts to earn money off of their knowledge” is “give askers faster and better quality answers”. I believe these 2 aims fit together naturally.

Scribu offers this review of our site:

What you’re paying for is not the knowledge itself – you could get that free of charge, by asking in the WordPress.org support forums. But by asking on WP Questions, you have a higher chance of getting a timely answer from one or more knowledgeable people, whereas in the WP.org forums your question could simply be overlooked or be given a half-baked answer.

I really think this could work. With the ever-growing user base of WordPress, a need for reliable, high-quality support is starting to appear. See WP Help Center for instance.

It was my frustration with “half-baked answers” that lead me to start thinking about a site like this. As I wrote in the very first post:

And yet, over the years, I’ve had a lot of bad experiences with free forums. I find it frustrating when I post a question that is altogether unique, but someone mistakes it for a common question, and so the only reply I get is “RTFM!!!!!” When people offer you free help, sometimes they are wonderful, but sometimes they attack you for aspects of your project that are beyond your control. For instance, I was once asked to fix a Javascript slideshow that depended on jQuery for functionality, and when I posted some of the code to a forum, the only response I got was “Do not use jQuery!” But it was the lead programmer on that project who had decided to use jQuery, and I didn’t have the power to change that. I only had the power to fix the problem that I had been assigned.

In the comments, I was confused by this:

I covered the site in July of 2008

We launched just 4 weeks ago, on December 8th of 2009. The domain name was previously owned by Leland of themelab.com. I was under the impression that he had never used the domain name for anything, but I could be wrong about that.

There’s a search field, but it would be nice if the site had a public archive of questions answered in the past.

This is good advice, and we’ve adopted the suggestion. The archives are now public. We may in the future adopt a mixed model.

Will WP Questions still be here next year?

I hope so! And I hope by then we’ve been able to roll out similar sites for MySql, PHP, Ruby, Groovy, Grails, etc.

Oliver Schlöbe writes up a plugin from code that he posted on WP Questions. In the spirit of our fundraiser, we expect to eventually have partially hidden archives, paid access, and we hope to pay royalty payments to peple who have offered answers like Oliver.

The Best of WPQuestions #2

Once again, we’d like to make available some of the most useful WordPress solutions we see posted at WPQ. This particular question was asked by user Meredith Marsh and answered with a clean bit of code by Utkarsh Kukreti who is also one of our Top Experts.

The Question

How can I list titles of posts in subcategories?
Example:Sports is a parent category. Soccer is a sub cat of Sports, as is Baseball. I have multiple posts in each (sub) category, and I need the title of those posts to appear as links, grouped by their sub category – with a heading of that sub category name.

This page should display all subcategories which are under the Sports parent category. This is not fixed- it needs to be flexible, so it can’t be hard coded. Even a query for the subcats can’t be hard coded because if there are no posts for Soccer, then soccer shouldn’t show up. If someone starts a frisbee team tomorrow, it needs to show up automatically.

Something like the following:

<h2>Sports</h2>

<h4>Baseball</h4>
<ul>
<li><a href="#">Baseball Team A</a></li>
<li><a href="#">Baseball Team B</a></li>
<li><a href="#">Baseball Team C</a></li>
<li><a href="#">Baseball Team D</a></li>
</ul>

<h4>Frisbee</h4>
<ul>
<li><a href="#">Frisbee Team A</a></li>
<li><a href="#">Frisbee Team B</a></li>
<li><a href="#">Frisbee Team C</a></li>
<li><a href="#">Frisbee Team D</a></li>
</ul>

<h4>Soccer</h4>
<ul>
<li><a href="#">Soccer Team A</a></li>
<li><a href="#">Soccer Team B</a></li>
<li><a href="#">Soccer Team C</a></li>
<li><a href="#">Soccer Team D</a></li>
</ul>

The Answer

What Utkarsh put together was very efficient:

<?php

if(is_page('Sports'))

{
$cat = 'Sports';
$catID = get_cat_ID($cat);
echo '<h2>' . $cat . '<h2>';
$subcats = get_categories('child_of=' . $catID);
foreach($subcats as $subcat)
{

		echo '<h4><a href="' . get_category_link($subcat->cat_ID) . '">' . $subcat->cat_name . ' <span>view only ' . $subcat->cat_name . ' →</a></h4>';
echo '<ul>';
$subcat_posts = get_posts('cat=' . $subcat->cat_ID);
foreach($subcat_posts as $subcat_post)

		{
echo '<li>';
$postID = $subcat_post->ID;
echo '<a href="' . get_permalink($postID) . '">';
echo get_the_title($postID);
echo '<span class="listing-entry organization">';
$postmeta = get_post_meta($postID, 'module', true);
echo $postmeta['organization'];
echo '</span><span class="read-more">(read more)</span>';
echo '</a>';
echo '</li>';

		}
         echo '</ul>';
	}
}
?>

Just change this line to change the category:

$cat = 'Sports'; 

WPQuestions Seeks Partnerships with Theme and Plugin Developers

We’re about to launch two new programs that would earn revenue for WordPress developers while increasing visibility and improving support for their projects. The ideal participants are developers who release their own free themes and plugins, but don’t have time to support them fully.

There would be two basic levels of engagement.

Option I: The Simple Fundraiser

This program would donate 25% gross profit to developers each time a question pertaining to their theme/plugin is posted to WPQuestions. The developer would simply need to contact us and provide a few keywords (ex: “Mimbo theme”) and a PayPal address. Payments would be sent at month’s end with no further participation required from the developer—it’s simply a donation.

This option is meant to…

  • Benefit the developers and community. WPQuestions wants to boost awareness of the best and most cutting-edge themes and plugins so that developers can profit more from their work. Plenty of developers answer questions for free, but we’d like to see them get both recognition and revenue so those projects stay part of the WP ecosystem longer and still feel worth the developer’s time.
  • Build relationships. We want to learn more about what WP developers need. Some have expressed interest in a referral system for projects involving their themes/plugins. Using Mimbo as an example, there are 170,000 active users, many who’ve built Mimbo-based sites for years and could be considered an expert. Since I personally have no time to build Mimbo-based sites for clients, I would love to keep track of “Mimbo Experts” who establish themselves at WPQ. Experts can also use their WPQ successes as proof-of-experience when soliciting clients.

Option II: The Featured Affiliate

This program would donate 100% gross profit to developers each time a question pertaining to their theme/plugin is posted to WPQuestions. The developers would be asked to include a graphic on their site suggesting WPQ to users in need of urgent answers, in addition to emailing us their keywords and a PayPal address. We would also encourage these developers to blog and tweet about WPQ for our mutual benefit.

This option is meant to…

  • Increase site activity and reciprocal traffic. We hope that more affiliate partnerships will lead to more questions posted and more enthusiasm from developers, which will lead to more success for Experts who wish to build their reputations as consultants.
  • Solve unanswered questions. In an ideal world, all WP-related questions deserve an answer, whether it’s for free at the developer’s own forum or for money by an Expert at WPQ. We hope a simple graphic like what’s at the Darren Hoyt Dot Com forums will encourage users to choose the latter when necessary….

    ad-affiliate

    …especially if the question is particularly urgent or complex enough. The more options available to users in need, the better.

Get Involved!

If either of the two programs above sound like they would benefit you as a developer, drop us an email and let us know. Feel free to ask questions or make suggestions below.

Congratulations to Darfuria, our first Expert to win over $100

Congratulations to Darfuria who just became our first Expert to win over $100! We hope within the next few months we’ll be offering congratulations to the first Expert to earn over $1,000.

WPQ is a true partnership—in the eyes of our customers, it is only as good as the Experts who answer the questions here.

Since launching, we’ve spent the first few weeks adding features and getting the kinks out. We now plan to ramp up our marketing which will bring in a lot more prize money for the many Experts who have participated in WPQ so far.

How to Ask a Question on WP Questions

For new users, we wanted to put up a brief tutorial on the process of asking a new question and making payment arrangements through PayPal.

In Step 1, you are prompted for a Question Title (65 characters or less) and Extended Explanation:

wpq_step1b copy

Our aim is to create a market for WordPress help, so that you can ask your WordPress questions and get fast, expert information. Please explain your problem with enough detail that the Experts can answer accurately. If you’ve forgotten a crucial detail, you can always edit your question later.

In Step 2, you can add optional tags or upload a screenshot:

wpq_step2 copy

The tags help our Experts determine if your question is in their area of expertise. The image-upload is useful to show Experts, visually, what kind of problem you’re having.

In Step 3, you can set the dollar amount for your question.

wpq_step3 copy

As a ballpark estimate, many Experts will want at least $1 for each minute they work. For the example question above, I think my problem might take 30 minutes for someone to figure out, so I offer $30 as a prize. (I’m actually re-posting an old question that Darren has already asked.)

In Step 4, you’ll see a summary of your question and the overall charges. Our fees are 9% of the prize that you’ve offered, plus 50 cents. This on top of the prize you are offering, so a prize of $30 leads to a total cost of $33.20.

wpq_step4 copy

Click the “Pay” button and you’ll be taken to PayPal to pay:

wpq_step_paypal copy

Once you’ve paid on PayPal, you are redirected back to our site.

wpq_step_return_from_paypal copy

But wait, is the question showing up? We rely on PayPal to ping our servers and say “This transaction was good”. This takes several seconds, and sometimes you will return to our site before PayPal has pinged our servers. Just wait a few seconds, and then hit refresh. Now you will see that your question is active:

wpq_step_final copy

As soon as your question goes live, we update our RSS feeds and we also post a link on Twitter, instantly notifying the many people who might be able to help you.

Once you’ve gotten some good answers, you will want to choose a winner. See the previous tutorial to understand how to assign the prize money to the experts who’ve answered your question.

Get started and ask your WordPress question now!

How to Assign Prize Money to Experts

To address some recent questions about how WPQuestions works, we’ve written the first of a few tutorials. Below, we demonstrate what happens after you, the Asker, has logged in and asked a question.

Here is your sample question:

wpq_pick_a_winner_question_show copy

As the Asker, you will see this button next to each answer (login required):

wpq_pick_a_winner_button

It doesn’t matter who you chose as the winner, since you get to change your mind on the next screen. You’ll get the option of giving all the money to one Expert, or you can divide the money among multiple Experts:

wpq_choosing_several_winners

If you click the option to “Divide the prize money amongst multiple Experts“, the form will expand to reveal every answer that has been posted. Use the dropdown boxes to divide the prize money accordingly:

wpq_choose_amount

The money you award to the Experts needs to equal the prize money you’ve paid for this question. Otherwise you get an error message like this:

wpq_pick_a_winner_money_doesn't_equal_prize copy

When the money has been portioned out accurately, the money will be sent and you’ll see this message:

wpq_pick_a_winner_money_sent copy

New Feature: Email Updates

To make WPQuestions more time-sensitive, we’ve added a few email update features. Askers will now receive emails whenever:

  1. Answers are given. As soon as new answers are posted, the Asker will receive email updates that say:

    [Username] has posted a possible answer. They wrote: [Answer]

  2. Answers are edited. As mentioned previously, Experts can now change their answer when necessary. Askers receive this update via email as a safeguard against potential abuse of the site. This means Experts cannot fudge their answer based on someone else’s response without the Asker finding out about it:

    You asked ‘[Question]‘. [Username] previously posted an answer but has decided to change their answer. This is the new version of their answer: [Answer]

  3. Questions expire. If an Asker forgets to select a best answer, or feels there is no best answer, the contest will expire after three days and they will receive this email:

    This question has now expired. We need for you to log in and chose which answer, or answers, deserves the prize money. If you do not make a choice by [Date] we will chose the winners. If you feel that none of the offered solutions actually answered your question, then please contact us.

A Change in Pricing Policy

We’ve decided to experiment with pricing for awhile to see how it affects participation in the site. As I mentioned in another post, Askers can now post question for as little as $4. We decided that instead of imposing a minimum price based on our own idea of standards, we should let that happen organically by negotiation between the Asker and the Expert.

Over time, it will hopefully become clear what questions are actually worth. I would say I don’t recommend questions to be asked at the $4 rate unless they are absolutely beginner-level or unless the Asker is comfortable taking the chance on getting less-than-expert-quality answers. My guess is that questions below $15, say, will mostly be ignored by more experienced PHP developers.

In this scenario, it’s perfectly ok for Experts to comment and ask that Askers post the question again at a higher price, especially if the solution requires more than a couple minutes of figuring.

We are relying on the audience to communicate and participate more and make clear what their expectations are. As the FAQ page says, we don’t want anyone feeling they’re contributing time and effort to questions that aren’t worth the money. Experts should feel free to make polite suggestions if they feel questions aren’t being valued. We will monitor this over the next month or two and see how it goes.

The Best of WPQuestions #1

As regularly as we can, we will be posting a “Best of WPQ” which re-publishes recent Expert contributions that we think are especially innovative, useful or noteworthy.

Currently, expired questions are only visible, via the control panel, to people who participated in them. Eventually we will implement a subscription-based plan for users who want searchable access to the entire archives, plus other valuable features.

The Question

On Saturday afternoon, I ran across an issue and rather than tinkering with the code myself, I thought it would be a great candidate for WPQ. The question received 10 possible answers, most of which contained potentially usable solutions, while some plain didn’t work.

Question: How do I insert this conditional inside my widgetized area?
For my Gravy theme, I’d like to insert some optional widgets in the footer. And I need all of them wrapped in a bounding div (“footer-widgets”), but only if widgets are activated.

In theory it would be something like this:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widgets') ) : ?>
<div id="footer-widgets"><!--widget stuff--></div>
<?php endif; ?>

But that obviously doesn’t work. Any ways to do this with minimal code?

The Answer

Justin Tadlock posted very soon after the question went live, and while his code and reasoning were correct, another user had to clarify exactly how to implement it. I decided to split the prize money. Since Justin answered first in the thread, he received a bigger portion. When the answers were combined and explained, they worked great, and here’s the solution (paraphrased):

An ID would need to be included when registering the sidebar and executing is_active sidebar. It can be a simple integer, like the number ‘1′. So this can be added to functions.php:

if ( function_exists('register_sidebar') )
    register_sidebar(array(
		'name' => 'Footer Widgets',
		'id'=> '1',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>',
    ));

While the template code in your footer will be like this:

<?php if ( is_active_sidebar( '1' ) ) { ?>
	<div id="footer-widgets">
		<?php dynamic_sidebar( 'Footer Widgets' ); ?>
	</div>
<?php } ?>