An interactive quiz WordPress block demonstrating client-side routing using the WordPress Interactivity API router functionality with regions.
This plugin creates an interactive quiz block that leverages WordPress's Interactivity API to provide seamless client-side navigation between quiz questions without full page reloads. It demonstrates advanced use of router regions to maintain state and provide a smooth user experience.
- Client-Side Routing: Navigate between quiz questions without page refresh using
@wordpress/interactivity-router - State Management: Tracks visited questions and maintains quiz state across navigation
- Router Regions: Uses
data-wp-router-regionto update specific content areas dynamically - Random Question Selection: Automatically selects random questions from a pool for each quiz session
- Time Limit Support: Configurable time limits for quiz completion
- Visual Feedback: Shows visited questions with different styling
The quiz block uses the following key components:
-
PHP Rendering (
render.php):- Generates random question selection server-side
- Sets up initial state with
wp_interactivity_state() - Creates router region for dynamic content updates
-
JavaScript View (
view.js):- Manages client-side state for visited questions
- Handles navigation events with
withSyncEvent - Updates UI based on quiz progress
data-wp-interactive: Establishes the interactive namespacedata-wp-router-region: Defines content area for client-side updatesdata-wp-watch: Watches for changes and triggers callbacksdata-wp-on--click: Handles click events for navigationdata-wp-bind--href: Dynamically binds href attributesdata-wp-class--is-visited: Conditionally applies CSS classesdata-wp-text: Updates text content dynamically
-
Clone or download this repository into your WordPress plugins directory:
cd wp-content/plugins/ git clone [repository-url] interactivity-router-region-quiz -
Install dependencies:
npm install
-
Build the block:
npm run build
-
Activate the plugin in your WordPress admin panel
- Create quiz question pages (e.g.,
/question-1,/question-2, etc.) - Add the "Interactivity Router Region Quiz" block to your quiz page and question pages
- Configure the time limit in the block settings
- The block will automatically:
- Select random questions from the pool
- Track visited questions
- Enable smooth navigation between questions
- WordPress 6.1 or higher
- PHP 7.0 or higher
- Node.js and npm
# Install dependencies
npm install
# Start development build with watch mode
npm run start
# Create production build
npm run build
# Run linting
npm run lint
# Format code
npm run formatinteractivity-router-region-quiz/
├── build/ # Compiled assets
│ └── quiz/
│ ├── block.json # Block metadata
│ ├── render.php # Server-side rendering
│ ├── view.js # Client-side interactivity
│ └── style-index.css # Block styles
├── src/ # Source files
│ └── quiz/
│ ├── edit.js # Block editor interface
│ ├── index.js # Block registration
│ ├── style.scss # Block styles (source)
│ └── view.js # Client-side logic (source)
├── plugin.php # Main plugin file
├── package.json # Node dependencies
└── README.md # This file
- Initial Load: When the quiz page loads, the PHP renderer selects random questions and initializes the state
- Navigation: Clicking on question links triggers the Interactivity Router's
navigateaction - Content Update: Only the router region content updates, maintaining the rest of the page structure
- State Persistence: The JavaScript state tracks visited questions across navigation
- Visual Feedback: Visited questions are marked with a CSS class for styling
- WordPress Interactivity API Documentation
- Interactivity API Examples
- Block Editor Handbook
- WordPress Interactivity Router Package
GPL-2.0-or-later
Contributions are welcome! Please feel free to submit issues or pull requests.
Created as an example for the WordPress block development community.