LifterLMS Unit Testing and CI for Add-ons: A Practical Setup
Set up LifterLMS unit testing for a custom add-on with the official lifterlms-tests framework: bootstrap, factories, a first test, testing against…
Read articleLifterLMS core ships with Multiple Choice, Picture Choice and True or False questions, and the paid Advanced Quizzes add-on adds types such as Fill in the Blank, Reorder Items, Short Answer and File Upload. Sometimes you need something neither offers: a numeric answer with a tolerance, a formula, a hotspot on an image, or a question tied to your own data. A LifterLMS custom question type lets you add it without touching core.
This guide is for PHP and JavaScript developers. It explains the four pieces every question type needs (registration, template, client-side validator and grading), shows where each one hooks into LifterLMS 10.x, and gives you a complete plugin that adds a Numeric Answer question graded within a tolerance.
If you only need to choose between the existing types, our overview of LifterLMS quiz question types is the better starting point.
A question is an llms_question post whose question_type meta names its type. The type itself is not a class; it is an array of settings returned by the llms_get_question_types filter. LifterLMS loads its own types into that filter at priority 5, merging each one with a set of defaults from LLMS_Question_Types::get_model().
The settings that matter most:
| Key | What it controls |
|---|---|
id, name, icon | The type's identifier, its label in the Course Builder, and a Font Awesome 4 icon name |
group | Which heading the type appears under in the builder's question menu, and the order of that heading |
choices | Whether the question has choices, and if so how many, whether several can be correct (multi), whether a choice can be marked correct (selectable), and text or image choices |
default_choices | Choices pre-filled when an instructor adds the question |
grading | auto, conditional, manual or false |
points, description, image, video, clarifications | Which standard features the builder shows for the question |
When a student takes a quiz, four things happen. LifterLMS renders the question with the template quiz/questions/content-{type}.php. The quiz script calls a JavaScript validator registered for the type to collect and check the answer. The answer is sent to the server, where LLMS_Question::grade() decides whether it is correct. Finally, results screens display the answer and the correct answer. A custom type has to take part in all four.
llms_get_question_types filter.lifterlms_locate_template filter, to load your template from your plugin. A theme can still override it, because the template name follows the core pattern.window.llms.quizzes.register_validator( type, callback ) in JavaScript. The callback receives the question's jQuery element and returns an object with valid (true, or an error message) and answer (an array of strings).llms_{type}_question_pre_grade. Return 'yes' or 'no' to bypass core grading; return null to let core grade. A matching llms_{type}_question_grade filter runs afterwards if you only need to adjust the result.Results screens use the filters llms_quiz_attempt_question_get_answer_array and llms_quiz_attempt_question_get_correct_answer_array when you need to change how answers are shown.
The plugin below adds a Numeric Answer type. The student types a number; it is correct if it falls within a tolerance of the correct value, so 9.81 and 9.8 can both be accepted for gravity.
The design choice that keeps it small is storing the correct answer as the question's single choice. The type declares exactly one selectable text choice, so the Course Builder shows one field with its correct toggle, already switched on. Instructors type the correct number into that field. No custom builder code is needed.
<?php
/**
* Plugin Name: LLMSX Numeric Question
* Description: Adds a "Numeric Answer" question type to LifterLMS quizzes.
*/
defined( 'ABSPATH' ) || exit;
const LLMSX_NUMERIC_TYPE = 'llmsx_numeric';
// 1. Register the type. One text choice holds the correct number.
add_filter( 'llms_get_question_types', function ( $types ) {
$model = LLMS_Question_Types::get_model();
$type = array(
'id' => LLMSX_NUMERIC_TYPE,
'name' => esc_html__( 'Numeric Answer', 'llmsx' ),
'icon' => 'calculator',
'group' => array(
'order' => 15,
'name' => __( 'Custom Questions', 'llmsx' ),
),
'choices' => array(
'min' => 1,
'max' => 1,
'multi' => false,
'markers' => array( 'A' ),
),
'default_choices' => array(
array(
'choice' => '0',
'correct' => true,
'marker' => 'A',
),
),
);
$type['choices'] = wp_parse_args( $type['choices'], $model['choices'] );
$types[ LLMSX_NUMERIC_TYPE ] = wp_parse_args( $type, $model );
return $types;
}, 20 );
// 2. Front-end template for the question.
add_filter( 'lifterlms_locate_template', function ( $template, $template_name ) {
if ( 'quiz/questions/content-' . LLMSX_NUMERIC_TYPE . '.php' === $template_name ) {
$template = plugin_dir_path( __FILE__ ) . 'templates/content-numeric.php';
}
return $template;
}, 10, 2 );
// 3. Client-side validator, registered with the LifterLMS quiz script.
add_action( 'wp_enqueue_scripts', function () {
if ( ! is_singular( 'llms_quiz' ) ) {
return;
}
$js = "window.llms.quizzes.register_validator( '" . LLMSX_NUMERIC_TYPE . "', function( \$question ) {
var val = ( \$question.find( 'input.llmsx-numeric' ).val() || '' ).trim();
if ( '' === val || isNaN( Number( val.replace( ',', '.' ) ) ) ) {
return { valid: 'Please enter a number.', answer: [] };
}
return { valid: true, answer: [ val ] };
} );";
wp_add_inline_script( 'llms-quiz', $js );
}, 20 );
// 4. Server-side grading with a tolerance, bypassing choice matching.
add_filter( 'llms_' . LLMSX_NUMERIC_TYPE . '_question_pre_grade', function ( $grade, $answer, $question ) {
$choices = $question->get_choices();
$given = isset( $answer[0] ) ? str_replace( ',', '.', trim( (string) $answer[0] ) ) : '';
if ( empty( $choices ) || ! is_numeric( $given ) ) {
return 'no';
}
$correct = (float) str_replace( ',', '.', $choices[0]->get( 'choice' ) );
$tolerance = (float) apply_filters( 'llmsx_numeric_tolerance', 0.01, $question );
return ( abs( (float) $given - $correct ) <= $tolerance ) ? 'yes' : 'no';
}, 10, 3 );
// 5. Show the number the student typed on results screens, not a choice lookup.
add_filter( 'llms_quiz_attempt_question_get_answer_array', function ( $ret, $answers, $question ) {
if ( $question && LLMSX_NUMERIC_TYPE === $question->get( 'question_type' ) ) {
$ret = array_map( 'sanitize_text_field', (array) $answers );
}
return $ret;
}, 10, 3 );
Save the template as templates/content-numeric.php inside the plugin:
<?php
/**
* Numeric Answer question template.
*
* @var LLMS_Question $question
* @var LLMS_Quiz_Attempt $attempt
*/
defined( 'ABSPATH' ) || exit;
$saved = ( isset( $attempt ) && $attempt ) ? $attempt->get_question_answer( $question->get( 'id' ) ) : array();
$value = ( is_array( $saved ) && isset( $saved[0] ) ) ? $saved[0] : '';
$id = 'llmsx-numeric-' . $question->get( 'id' );
?>
<div class="llms-question-numeric">
<label class="sr-only" for="<?php echo esc_attr( $id ); ?>">
<?php echo esc_html( wp_strip_all_tags( $question->get_question( 'html', $attempt ) ) ); ?>
</label>
<input class="llmsx-numeric" id="<?php echo esc_attr( $id ); ?>" type="text" inputmode="decimal"
autocomplete="off" value="<?php echo esc_attr( $value ); ?>">
</div>
The type is merged with LLMS_Question_Types::get_model() exactly as core does with its own types, so every setting you leave out gets the same default. It appears under a "Custom Questions" heading in the builder's question menu. The filter runs at priority 20, after core's types have loaded at 5.
LifterLMS asks for quiz/questions/content-llmsx_numeric.php. The lifterlms_locate_template filter points that name at the plugin's file. The wrapper LifterLMS prints around every question carries data-type="llmsx_numeric", which is how the quiz script knows which validator to call. The template reads any saved answer with $attempt->get_question_answer(), so resumed attempts and the Previous button show what the student typed before.
The inline script is attached to the core llms-quiz script handle with wp_add_inline_script(), which by default prints it after the quiz script, once window.llms.quizzes exists. Returning a string as valid makes the quiz show that message and stay on the question.
Because the type has choices and automatic grading, core would normally compare the submitted choice IDs with the correct choice. The pre_grade filter replaces that comparison with a numeric one. Tolerance defaults to 0.01 and can be changed per question through the plugin's own llmsx_numeric_tolerance filter. Points, quiz grade, pass or fail, lesson completion and engagements then work exactly as for any core question.
For choice-based types, core turns each stored answer into the text of the matching choice. Our stored answer is the typed number, not a choice ID, so the get_answer_array filter returns it as text instead. It is sanitised there, because answers are stored as submitted. The correct answer shown to students comes from the choice text without any extra code.
conditional grading compares answers with a stored correct value, ignoring case by default (the llms_quiz_grading_case_sensitive filter changes that). Note that the core builder has no field for that value; the Advanced Quizzes add-on provides the editing interface for its own conditional types.grading to manual and the attempt waits for an instructor to grade it under Reporting, as with essay-style questions. See our guide to quiz attempts, grading and results.pre_grade. Keep it fast, set a timeout, and decide what happens when the service is down.pre_grade callback is plain PHP and easy to test with commas, spaces, negative numbers and empty strings.Yes. The question type API is in the free core plugin; the example above needs nothing else.
Yes. Use llms_{type}_question_pre_grade or llms_{type}_question_grade, for example llms_choice_question_grade for Multiple Choice.
Questions of any registered type are stored as normal question posts, so they can be read through the questions endpoint. Check that your type's data makes sense to whatever reads it.
The builder offers PHP actions inside the question editor and a JavaScript hooks system, but custom question settings require builder JavaScript. Reusing choices, as the example does, avoids that work for many types.
Write down exactly how the question should be answered and graded, including edge cases, before writing code; that specification decides which grading mode you need. If your assessment needs a question type, scoring model or quiz experience LifterLMS does not offer, our LifterLMS plugin development team builds and maintains custom quiz features. Describe the question you need and we will tell you how we would build it.
Set up LifterLMS unit testing for a custom add-on with the official lifterlms-tests framework: bootstrap, factories, a first test, testing against…
Read article
What a headless LifterLMS setup really involves: which parts the REST API covers, why lessons and completion need custom endpoints, how to…
Read article
How the LifterLMS engagement engine works, how to register a custom engagement trigger that admins can configure, how to fire it reliably, and how to…
Read articleSkip the trial and error. Get specialist LifterLMS help and ship faster.