PA Learning Test Plugin
Build a minimal WordPress plugin that demonstrates your ability to work with WordPress core APIs, hooks, and coding standards
by Felopater Adel · github.com/felopateradel/pa-learning-test-plugin · website
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-learning-test-plugin/archive/refs/heads/main.zipPA Learning Test Plugin – Explanation Document
- Identifying and Using WordPress Hooks This plugin integrates with WordPress using core action hooks and shortcodes instead of executing logic directly. The following hooks were identified and used based on the plugin’s requirements: • admin_menu Used to add a settings page under the WordPress Settings menu. This hook ensures the menu is registered only when the admin interface is being built. • admin_init Used to register settings, sections, and fields using the WordPress Settings API. This hook runs at the correct time to safely register options and their sanitization rules. • Shortcode API (add_shortcode) Used to expose the saved option value on the front-end through the [pa_learning_message] shortcode. Hooks were chosen by: • Understanding when WordPress initializes admin pages and settings • Mapping each feature to the appropriate lifecycle stage • Avoiding direct execution of code outside WordPress’s control flow This approach ensures compatibility with WordPress core and other plugins.
- Plugin Structure and Code Organization The plugin follows a class-based (OOP) structure with a single main class: PA_Learning_Test_Plugin. Key structural decisions: • The main plugin file acts as the entry point • All logic is encapsulated inside a class to avoid global scope pollution • Hooks are registered inside the class constructor • Related functionality (settings, rendering, sanitization, shortcode output) is grouped into dedicated methods This structure: • Improves readability and maintainability • Prevents function name collisions • Makes the plugin easy to extend in the future The plugin instance is initialized once at the bottom of the file, ensuring all hooks are registered when WordPress loads the plugin.
- Settings API Usage The plugin uses the WordPress Settings API instead of handling form submission manually. Key components: • register_setting() to register the option and define sanitization • add_settings_section() to logically group settings • add_settings_field() to render the input field • settings_fields() and do_settings_sections() to handle nonce security and form output automatically This ensures: • Proper nonce verification • Automatic option handling • Consistency with WordPress admin UI behavior
- Sanitization and Escaping Security best practices are applied following WordPress guidelines. Sanitization (Input) • User input is sanitized using sanitize_text_field() via the sanitize_callback • Sanitization occurs before the value is saved to the database • This prevents malicious or unexpected data from being stored Escaping (Output) • All dynamic values are escaped at the time of output • Examples include: ◦ esc_attr() for input field attributes ◦ esc_html() for displayed text • The shortcode output also escapes data before returning it This follows the WordPress security principle: Sanitize on input, escape on output.
- Shortcode Implementation The plugin provides a shortcode [pa_learning_message] that outputs the saved value. Design considerations: • Uses WordPress Shortcode API • Retrieves the saved option using get_option() • Escapes the output to ensure front-end safety • Keeps shortcode logic isolated in a dedicated method This allows the plugin to safely expose admin-configured data on the front end.
- How This Differs from Plain PHP This plugin differs significantly from a plain PHP implementation: WordPress Plugin Plain PHP Uses hooks to control execution Code runs immediately Uses Settings API Manual form handling Built-in sanitization & escaping Custom validation logic Encapsulated class structure Global functions & variables WordPress lifecycle aware No framework lifecycle
In WordPress development:
• Code responds to events instead of running sequentially
• Core APIs handle security, persistence, and extensibility
• Plugins extend WordPress rather than replacing its architecture
- Summary
This plugin demonstrates proper WordPress development practices by: • Using hooks and shortcodes to integrate with core • Structuring the plugin using OOP principles • Leveraging the Settings API for secure option handling • Applying correct sanitization and escaping • Avoiding plain PHP patterns in favor of WordPress APIs
The result is a secure, maintainable, and WordPress-compliant plugin.