Hook & Filter Architecture
Overview
The wordpress repository utilizes the standard WordPress event-driven architecture, allowing developers to extend and modify core functionality without altering the base source code. This is achieved through two primary mechanisms: Actions and Filters.
- Actions (Hooks): Allow you to trigger custom code at specific points during the execution cycle.
- Filters: Allow you to intercept and modify data before it is processed or rendered.
Action Hooks
Actions are used to perform tasks (like logging, sending emails, or injecting scripts) when a specific event occurs.
Usage
To hook into an action, use the add_action() function.
/**
* Example: Triggering a custom function during project initialization.
*/
add_action('ua_init_mvp_feature', 'handle_custom_initialization', 10, 1);
function handle_custom_initialization($feature_data) {
// Your custom logic here
error_log('Feature initialized: ' . $feature_data['name']);
}
Common Action Hooks
| Hook Name | Description | Parameters |
| :--- | :--- | :--- |
| ua_before_context_render | Fires before the main MVP context is rendered. | (array) $context_params |
| ua_after_module_load | Fires immediately after a specific module is loaded. | (string) $module_id |
| ua_on_data_sync | Fires when data synchronization completes. | (bool) $is_success |
Filter Hooks
Filters are used to manipulate variables or data structures. Your callback function must return a value.
Usage
To modify data using a filter, use the add_filter() function.
/**
* Example: Modifying the default configuration array.
*/
add_filter('ua_base_configuration', 'customize_mvp_config', 10, 1);
function customize_mvp_config($config) {
$config['debug_mode'] = true;
$config['api_timeout'] = 30;
return $config; // Always return the filtered value
}
Common Filter Hooks
| Hook Name | Description | Expected Return Type |
| :--- | :--- | :--- |
| ua_filter_output_data | Modifies the final array before output. | array |
| ua_content_template_path | Overrides the file path for the content template. | string |
| ua_is_feature_enabled | Dynamically enables/disables features. | boolean |
Technical Specifications
Input Parameters
When hooking into these interfaces, the following standard parameters apply to both add_action() and add_filter():
- Tag (
string): The name of the hook to join. - Callback (
callable): The function to be run. - Priority (
int): (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers = earlier execution. Default is10. - Accepted Arguments (
int): (Optional) The number of arguments the function accepts. Default is1.
Best Practices
- Prefixing: All custom hooks in this repository are prefixed with
ua_to prevent collisions with WordPress core or other plugins. - Return Types: When using filters, ensure the returned data type matches the input data type to prevent runtime errors in the core logic.
- Anonymous Functions: While supported, using named functions or class methods is recommended for better compatibility with
remove_action()orremove_filter()calls.
Internal Event Dispatchers
Note: These functions are used internally by the core to trigger the hooks listed above. Developers should use the public add_action and add_filter interfaces instead.
do_action(): Invokes the callbacks attached to an action hook.apply_filters(): Invokes the callbacks attached to a filter hook and returns the modified value.