Integrating Multiple Choice Quizzes in WordPress - Creating the Backend

Multiple choice questions are something that most of us have faced at least once in our life. We love them because we can provide correct answers by logically thinking about provided possibilities, even if we don't exactly know the correct answer. Also answering takes less time which makes it so popular.

Creating a multiple choice quiz in WordPress can be a very exciting and profitable task. You can use it in your personal blog to attract more visitors, or you can create a premium section with advanced quizzes, or you can create quizzes focusing on popular certification exams. There are numerous possibilities for making it profitable.

This plugin won't take you through to creating quizzes for building a business, but we have to start somewhere to get where we want to go. So this would be a good start for creating quizzes and making profits out of it.

So let's get started.


Functionality of the Plugin

First we need to gather the requirements of the plugin before going to design or implementation. Basically, we should be able to create multiple choice quizzes and allow users to take the quizzes and get results.

So let's take a look at the detailed requirements and components of our plugin:

  • The plugin should have a backend where we can insert questions and answers dynamically. Also it's ideal to have quiz categories to group quizzes into specific sections.
  • Site admin should be able to configure the quizzes through a settings page.
  • Users should have a frontend where they can take a quiz.
  • Once a quiz is completed users should be able to get the scores and results.

This tutorial is going to be constructed as a two part series, where we develop the backend of the plugin in the first part, followed by the plugin frontend in the second part.


Planning the Plugin Backend

In this part we are going to focus on developing the backend of the plugin where we develop the required data for quizzes.

Usually there is a list of quizzes, each of them containing a specific list of questions. With this plugin we are not going to create quizzes. We will be creating individual questions and assigning them dynamically to quizzes when requested.

Now we can take time to identify the components required for implementing the backend as listed in the following section.

  • Questions need to be created in the backend with the answers. A custom post type will be the best solution for implementing the questions. So we are going to use a custom post type called wptuts_quiz.
  • Each question should have multiple answers and one correct answer. Fields for answers will be created inside a meta box in the custom post creation screen.
  • Questions will be categorized into various sub-topics hence we need a custom taxonomy called quiz_categories for the wptuts_quiz post type.
  • Then we need to validate the question creation process. We will be using client side jQuery validations when necessary in question creation.
  • Finally we need a plugin settings page to store the number of questions per quiz and the duration for a quiz.

Having identified the necessary WordPress components, we can directly move into implementing the backend of the quiz plugin.


Creating Questions

The question creation process consists of four main sections: defining custom post type, defining custom taxonomy, assigning custom fields, and validations. Each of these sections will be discussed with detailed code in the upcoming sections.

1. Creating the Questions Custom Post Type

We need the most basic form of custom post type for questions without any advanced configurations. The only thing we need to consider is the selection of the right field for the question.

The default post creation screen contains two fields for title and content. You can choose any of those fields for the question. I am going to choose concatenation of both title and content fields considering advanced possibilities.

We will be creating an object-oriented plugin instead of a functional plugin, so all of the necessary actions, shortcodes and initializations will be done inside the constructor. The following contains the code for implementing the wptuts_quiz post type.

We have enabled both title and editor fields in the custom post type to use for the question. Since the functions are located inside a class, we have to use $this in our WordPress actions, filters, and shortcodes to call the functions.

Apart from that all the parameters mentioned in the code are initialized with their default values.

2. Creating the Question Categories

In order to group questions into specific sections, we need something similar to default WordPress categories. Hence we will be using a custom taxonomy called quiz_categories. We need to call the custom taxonomy generation function on the init action as we did earlier. So the constructor of our plugin class needs to be updated with the following code.

Then we can implement the wpq_create_taxonomies function on the wptuts_quiz post type as shown in the following code.

I have used the default option parameters to create this custom taxonomy. Once the plugin is activated your custom post type and taxonomy will be displayed as shown in the following screen.

3. Creating the Question Answers

Next, we have to create multiple answers for each question. In this plugin the maximum number of answers per single question will be limited to five.

You can dynamically assign 1-5 answers for any question. Also we need to specify the correct answer from the provided list of answers. Since this data is associated with our questions, we can use a meta box with custom fields to generate the necessary fields.

As usual we need to update the constructor with the following code:

Consider the following code for the implementation of meta boxes with answers fields.

4. Code Explanation

  • Answers of each question will be stored in a JSON encoded string in the post_meta table with the key _question_answers. So we access this field using the get_post_meta function to get the current values.
  • Then we get the correct answer of the question using a similar method. The correct answer will be stored as a string in the post_meta table with the key _question_correct_answer.
  • Finally, we create the HTML form, which contains the correct answer as a dropdown box and the possible answers as five text area fields.
  • All the existing values retrieved using the get_post_meta function will be assigned to the respective fields.

You should see something similar to the following screen, once you create the meta box.

quiz1

Now we have all the data required for our quiz generation plugin. Next step is to save the question data into the database.

But we need some validations prior to that. So let's move into validations.


Validating Question Creation

We don't have complex validation rules at this stage in the question creation process. Therefore we are going to use client side jQuery validations before submission.

Here we need the admin_enqueue_scripts action to be included into the constructor of our plugin.

So update the constructor with following code before we get started.

Now look at the following code for including necessary script files for validation.

Using wp_register_script and wp_enqueue_script, we have a plugin-specific JS file called quiz.js for handling validations. Validation will be done using the jQuery library and hence we have set the built-in jQuery library as a dependency for our plugin-specific JavaScript.

Having included the scripts, let's implement the actual validations in the quiz.js file as shown in the following code.

First, we assign an empty div element to the post creation screen to display any errors. Then we can call a custom JS function on the post publish action by checking the correct post type using the #post_type hidden field value.

The following code contains the implementation of the wpq_validate_quizes function.

Code Explanation

  • First we have to hide the error container and set its current error message to empty.
  • Then we check whether the title exists, as title is mandatory for questions.
  • Next we get the selected correct answer and check whether the answer field related to the correct answer is empty.
  • When validation errors are generated we display the errors in the specified error container and prevents the submission of the post.

The following image shows the post creation screen with custom validation messages.

wpquiz2

Once the form is successfully validated without errors, we can move onto saving the question details to the database.


Saving Question Details

WordPress provides an action called save_post, which will be executed just after the post creation. We can define a custom function on the save_post action to save the custom field details into the database.

As usual update the constructor with the save_post action code.

Implementation of the wpq_save_quizes function is given in the following code.

The post ID is passed automatically to this function. Initially we execute validations for nonce value and autosave. The most important thing before saving is the validation of post type.

If you omit the post type check, this code will be executed for all the post types in your WordPress installation including normal posts, which can result in inconsistent data.

Finally we get the values of our answers fields and correct answer field to be saved to the database using the update_post_meta function. All the custom fields details will be saved to the post_meta table.

Now we have completed the question creation process. Since we are using randomly generated quizzes, you might need lots of questions to see this plugin in action. So get ready by adding additional questions before we implement the quiz in the next part.


Quiz Settings Page

Even though it's not mandatory, it's ideal to have a settings page for our plugin so that the admin can customize according to their preferences.

So let's implement a simple settings page to contain the quiz duration and number of questions per quiz. We need to implement the admin_menu action in the constructor.

We can use the add_menu_page function to add a settings page to the admin area. The wpq_display_settings function is used to implement the display elements of the settings page.

We have used two text fields for duration and questions per quiz. Form submission and data saving can be handled manually using custom code, but WordPress provides us a simplified method for updating options.

In this method you have to set the form action to the options.php file. Then you have to create a hidden field called action to contain the value of 'update'. Finally we need to have another hidden field called page_options to contain the names of the two text fields to be updated.

Make sure to use these built-in options update techniques in scenarios where the requirements for saving the fields are not complex.

Once the submit button is clicked, form details will be updated automatically without any custom code.


What's Next

We have completed the backend of our quiz plugin. Now you can create questions and get ready for the next part where we will use these questions to dynamically generate quizzes.

Until then let me know the best possible way to implement a dynamic quiz on the frontend. Keep in mind that only one question will be displayed at a time. When the user requests a question, he can move to the next question.

Looking forward to your suggestions.

Tags:

Comments

Related Articles