PA Developer Test Plugin
Build a WordPress plugin demonstrating plugin architecture, database usage, and REST API integration.
by Felopater Adel · github.com/felopateradel/pa-developer-test-plugin
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/felopateradel/pa-developer-test-plugin/archive/refs/heads/main.zipTask 2 – Explanation
Plugin Structure The plugin follows a modular architecture with clear separation of concerns: Main Plugin File (pa-developer-test-plugin.php) • Defines plugin metadata (name, description, version, author) • Sets up plugin constants for paths and URLs • Loads required class files • Registers activation hook and initializes components Core Components (located in /includes/ directory): • class-activator.php: Handles database setup during plugin activation • class-admin-page.php: Creates and manages the admin interface • class-rest-api.php: Registers and processes custom REST API endpoints
Database Schema
A custom database table is used to store form submissions from the admin settings page. Each record contains a unique ID, the submitted text value, and a timestamp indicating when the entry was created. The table uses the WordPress database prefix to remain compatible with different installations.
Field Purposes: • id: Auto-incrementing unique identifier for each entry • entry_text: Stores user-submitted text data • created_at: Automatic timestamp recording when each entry was created Security & Sanitization Input Sanitization • Admin Form Input: All text submissions are sanitized using sanitize_text_field() • REST API Parameters: User IDs are sanitized with absint() to ensure positive integers • Database Operations: Uses WordPress $wpdb methods with format specifiers for type safety Security Measures • Capability Checks: Admin functions require manage_options capability • Nonce Verification: Form submissions are protected with WordPress nonces against CSRF attacks • Admin Referer Checks: Validates request origin using check_admin_referer() • Output Escaping: All displayed data uses esc_html() to prevent XSS vulnerabilities REST API Security • Parameter validation ensures only valid user IDs are processed • User existence is verified before returning sensitive data (email addresses) • Permission callback can be customized for production use (currently public for testing) Table Creation Management The plugin prevents table recreation on every activation using a conditional check: Key Features:
- Conditional Execution: Checks table existence before attempting creation
- Safe Table Creation: Uses WordPress dbDelta() function for reliable table management
- Activation-Only: Database setup only occurs during plugin activation, not on every load
- Prefix Compatibility: Uses $wpdb->prefix for multisite and custom prefix support
Plugin Workflow Activation Sequence:
- User activates plugin → WordPress calls register_activation_hook()
- PA_Test_Activator::activate() executes → Creates database table (if not exists)
- Table is ready for use → Plugin becomes operational Runtime Operation:
- WordPress loads → plugins_loaded hook fires
- Admin interface and REST API components initialize
- Admin can access Settings → "PA Test Plugin" to submit/view entries
- External systems can call REST endpoint at /wp-json/pa-test/v1/process 📡 REST API Endpoint Endpoint: POST /wp-json/pa-test/v1/process Functionality: • Receives user ID parameter • Validates and sanitizes input • Retrieves user data from WordPress database • Returns JSON response with user details or error message
Design Principles
- Separation of Concerns: Each class handles specific functionality
- WordPress Standards: Follows WordPress coding conventions and APIs
- Security First: Implements multiple layers of protection
- Maintainability: Clean, documented code with single responsibility classes
- Scalability: Architecture supports easy addition of new features
This architecture ensures the plugin is secure, maintainable, and follows WordPress development standards while providing the required functionality for testing and demonstration purposes.