Developer guide
RD Quiz is designed for WordPress add-ons. Use its hooks to add behaviour while leaving the core plugin unchanged. The complete hook reference is in Hooks.
Requirements and loading
RD Quiz requires WordPress 7.0 or later and PHP 7.4 or later. An extension can be a normal WordPress plugin. Load integration code on plugins_loaded after RD Quiz has loaded:
add_action(
'plugins_loaded',
function () {
if ( ! class_exists( '\\RDQuiz\\Questions' ) ) {
return;
}
// Register RD Quiz hooks here.
},
20
);
Use your own prefix or namespace for every function, class, option, REST route, and hook. Do not modify RD Quiz tables directly; use the services and hooks so that editor, REST, import/export, and front-end behavior remain consistent.
Architecture
| Layer | Responsibility |
|---|---|
src/Questions.php | Question validation, normalisation, storage, and type registration. |
src/Quizzes.php | Quiz storage, assignments, settings, pass mark, and question weights. |
src/Attempts.php | Answer sanitisation and scoring. |
src/ActionRunner.php | Executes non-blocking actions after the configured event. |
src/Rest/TakeController.php | Public quiz-taking API, gating, and retake policy. |
assets/js/questions/ | Question editor. |
assets/js/quizzes/ | Quiz editor and attempts list. |
assets/js/take/ | Visitor-facing quiz runner. |
Public quiz routes deliberately have no WordPress capability requirement, because guests can take quizzes. An extension must preserve the protections already provided by the take controller: do not expose answer keys before submission, verify ownership before revealing an attempt, and use the supplied nonce for browser writes.
Build an add-on safely
- Register a PHP hook to recognise and persist the feature.
- Add an editor control through the documented JavaScript filter when an author needs to configure it.
- Add front-end UI through the documented JavaScript filter when a visitor must interact with it.
- Sanitize all values at the PHP boundary, even if a JavaScript editor already validates them.
- Return safe defaults when your add-on is inactive or receives an unfamiliar record.
The core plugin stores unknown question settings and action settings only when their respective sanitizer or registry accepts them. This prevents a client from storing arbitrary data through the REST API.
Example: add a pass mark
This example applies an 80% pass mark only to quizzes whose ID is in your own configuration. It uses the existing result calculation, so attempts retain the normal score and percentage.
add_filter(
'rd_quiz_pass_mark',
function ( $mark, $quiz ) {
if ( 42 === (int) $quiz['id'] ) {
return 80;
}
return $mark;
},
10,
2
);
The returned value is clamped to 0–100 by RD Quiz. Return null when a quiz should have no pass/fail result.
Example: add a non-blocking action
This adds an audit_log action that makes an integration hook available after a quiz is submitted. The custom action is non-blocking, so it never prevents a visitor from seeing a result.
add_filter(
'rd_quiz_actions',
function ( $actions ) {
$actions['audit_log'] = array(
'label' => __( 'Send audit event', 'my-quiz-addon' ),
'description' => __( 'Notifies another plugin after submission.', 'my-quiz-addon' ),
'icon' => 'dashicons-chart-line',
'events' => array( 'attempt_submitted' ),
'defaults_to' => array( 'attempt_submitted' ),
'repeatable' => true,
'blocking' => false,
'fields' => array(),
);
return $actions;
}
);
add_filter(
'rd_quiz_run_action',
function ( $context, $action, $quiz, $event, $attempt_id ) {
if ( 'audit_log' !== $action['type'] ) {
return $context;
}
do_action( 'my_quiz_addon_audit', $context, $quiz, $attempt_id );
return $context;
},
10,
5
);
Use rd_quiz_action_should_run when an action has conditions. Use rd_quiz_action_satisfied and the rdQuiz.take.gate JavaScript filter only for a blocking action, such as payment or manual approval; an unrecognised blocking action stays closed by design.
Example: add a question setting in the editor
Enqueue a script on RD Quiz question screens, then use the WordPress hooks package. The callback receives the current values object and set function; patch values with set, rather than maintaining a separate form state.
import { TextControl } from '@wordpress/components';
import { createElement } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
addFilter(
'rdQuiz.questionForm.fields',
'my-quiz-addon/hint-field',
( fields, values, set ) => [
...fields,
createElement( TextControl, {
label: 'Learner hint',
value: values.settings?.hint || '',
onChange: ( hint ) =>
set( {
settings: { ...values.settings, hint },
} ),
} ),
]
);
Pair it with a PHP sanitizer. The following example accepts the setting only for a custom question type named hinted_choice:
add_filter(
'rd_quiz_sanitize_question_settings',
function ( $clean, $type, $raw ) {
if ( 'hinted_choice' !== $type || ! is_array( $raw ) ) {
return $clean;
}
return array(
'hint' => sanitize_text_field( $raw['hint'] ?? '' ),
);
},
10,
3
);
Test an extension
Test the complete lifecycle, not only a hook callback:
- Create and edit the configured record in the admin UI.
- Confirm the REST response contains only the data appropriate for that user.
- Take the quiz as a logged-out visitor.
- Test the result, repeated-submit behavior, and any action failure.
- Export and import a fixture if the extension relies on persisted settings.
For RD Quiz core development, run:
composer run lint
./vendor/bin/phpstan analyse --memory-limit=1G
composer run test
npm run lint:js
npm run build
Compatibility rules
- Treat hook arguments as read-only unless the hook explicitly expects you to return a changed value.
- Preserve existing array keys when filtering a registry or data payload.
- Return the incoming value unchanged for records your add-on does not own.
- Do not reveal
is_correct, a question answer key, or another visitor’s lead or attempt in public output. - Do not use the action shortcodes in normal post content. They are intentionally registered only while an email action is rendered, because they can expose private attempt data.