WP-Polls : How to fix wrong results in multiple answers poll

wp-poll multiple answers problem

WordPress plugin WP-Polls is a very popular poll plugin with more than 100,000+ active installs at the time of writing. It is really easy to use, but I have noticed a strange behavior when using a poll with multiple answers. When showing results for this type of poll, the total percentage of all the answers can give you a number bigger than 100%. In this article I will show you how to modify the source code in WP-Polls, so that the total sum of all the answers gives 100% for polls with multiple answers and leave the polls with single answers unaffected.

Identifying the problem

WP-Polls plugin supports two types of poll. You can have one which gives visitor option to select only one answer / choice (using radiobox controls) and a poll that gives visitors an option to select multiple answers / choices (using checkbox controls).

When showing the results of the poll, I want percentages of all the answers to sum to a number 100%, but this is not the case with the multiple answers type poll. The cause for this problem is the following line in the code:

$poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvoters)*100));

What the above code does is it divides the number of votes with the number of voters and not number of votes. With polls where visitors can give only one answer, this is not a problem because number of votes will always be the same as the number of voters, but this is not the case with polls, where visitors can choose multiple answers.

So lets see an example of a poll which allows multiple answers. It has 5 available options for visitor to choose from. The first visitor comes in and selects all 5 answers. What WP-Polls results gives you is this:

wp-polls results page

Each of the option show 100%, but this just seems wrong to me. I want the poll to show the percentages, where the sum of all the answers is equal to 100%, which is not the case in the above example. To fix this, we will need to modify the source code of this plugin and after the fix, each of 5 answers will show 20% with a total sum of 100%.

MyThemeShop - Premium WordPress Themes & Plugins
Choose between Blog, Magazine, WooCommerce, Business Themes.
  • Fully Responsive Designs
  • Premium Support 24x7
  • 30 Day Money Back Policy
  • Buy Once, Use for Lifetime
  • SEO Friendly
  • Use On Unlimited Sites
Check it now
This is an affiliate ad.
Note: For easier modifications of the code, if you don’t have it yet, install some sort of Code Editor plugin like Advanced Code Editor. Those plugins gives you search options, line numbers and other improvements that make job of changing the code much easier.

Modifying The Code

Let's change the behavior of this plugin. What we need to change in WP-Polls plugin is the way it calculates the results. Instead of using total voters, we will make the plugin to use total votes.

Remember: This code modification will not affect the results of polls which do not use multiple answers because in those polls, the number of votes and the number of voters is the same so they will remain unaffected.
  1. Go to the WordPress Dashboard and choose Plugins > Installed Plugins
  2. Find the WP-Polls plugin and click on Edit. The file wp-polls.php should open up in the editor.
  3. Find function display_pollresult and inside it locate the following line:
    $poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvoters)*100));
  4. Delete or comment out this line and replace it with:
    $poll_answer_percentage = round((($poll_answer_votes/$poll_question_totalvotes)*100));
  5. Now we need to do the same for the archive page. In the same wp-polls.php file search for function polls_archive() and inside it, find the following line:
    $poll_answer_percentage = round((($polls_answer['votes']/$polls_question['totalvoters'])*100));
  6. Replace this line with:
    $poll_answer_percentage = round((($polls_answer['votes']/$polls_question['totalvotes'])*100));
  7. Click on Update File button to save the changes.

This is it. If we refreshed that poll we had earlier, each of the answers would show 20% result and it would look like this:

wp-polls results page after code modification

Remember:Any manual modifications you do for any WordPress plugin will get lost after updating the plugin with a newer version. The way I deal with this is to create a single document where I write down manual code modifications and their line number for every plugin I modified and if any of these plugins need updated, I use this document to modify the code back in the plugin.

Conclusion

WordPress has many poll system plugins, but WP-Polls is popular and simple to use. With it you can create a poll where visitor chooses only one answer or a poll where they can choose multiple answers. With poll that allows more than 1 answer from users, the total sum of all results will be greater than 100%, but with only two modifications in the code you can fix this issue. After the modifications, each poll you make will have a sum total of 100%, regardless which type of poll you are using.

If you found this article useful, take a moment and share it on social networks or drop a comment below.

Share this page

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top