# Hooks & Filters

### Action Hooks

1. **`payment_page_after_payment`**
   * **Purpose**: Triggered after a payment is successfully processed.
   * **Usage**: Allows developers to execute additional actions like logging the transaction, notifying users, or updating external systems.
   * **Example**:

     ```php
     add_action('payment_page_after_payment', 'log_payment_details', 10, 2);

     function log_payment_details($transaction_id, $amount) {
         // Log the transaction details
         error_log("Transaction processed. ID: $transaction_id, Amount: $amount");
     }
     ```
2. **`payment_page_settings_saved`**
   * **Purpose**: Called after the plugin settings are saved.
   * **Usage**: Use this hook to perform actions like clearing caches, sending notifications, or validating settings.
   * **Example**:

     ```php
     add_action('payment_page_settings_saved', 'clear_cache_after_settings_change');

     function clear_cache_after_settings_change() {
         // Clear cached data after settings are updated
         my_cache_clear_function();
     }
     ```
3. **`payment_page_gateway_initialized`**
   * **Purpose**: Invoked when a payment gateway is initialized.
   * **Usage**: Perform setup actions, such as validating configurations or preparing necessary resources.
   * **Example**:

     ```php
     add_action('payment_page_gateway_initialized', 'check_gateway_configuration');

     function check_gateway_configuration($gateway) {
         // Check if gateway configuration is correct
         if (!isset($gateway['api_key'])) {
             // Handle missing API key
             error_log("API key is missing for the gateway.");
         }
     }
     ```
4. **`payment_page_before_payment`**
   * **Purpose**: Triggered before a payment is processed.
   * **Usage**: Useful for running validation checks or modifying the payment request before it's sent to the gateway.
   * **Example**:

     ```php
     add_action('payment_page_before_payment', 'validate_payment_request', 10, 1);

     function validate_payment_request($payment_data) {
         // Perform validation on payment data
         if ($payment_data['amount'] <= 0) {
             wp_die('Invalid payment amount.');
         }
     }
     ```
5. **`payment_page_gateway_disabling`**
   * **Purpose**: Executed when a payment gateway is being disabled.
   * **Usage**: Perform cleanup actions or notify users about the change.
   * **Example**:

     ```php
     add_action('payment_page_gateway_disabling', 'notify_gateway_disabling', 10, 1);

     function notify_gateway_disabling($gateway_name) {
         // Notify users when a gateway is disabled
         wp_mail('admin@example.com', 'Gateway Disabled', "$gateway_name has been disabled.");
     }
     ```

### Filter Hooks

1. **`payment_page_gateway_options`**
   * **Purpose**: Allows modification of the list of available payment gateways.
   * **Usage**: Customize which gateways are available to the user, adding or removing options as necessary.
   * **Example**:

     ```php
     add_filter('payment_page_gateway_options', 'add_custom_gateway', 10, 1);

     function add_custom_gateway($gateways) {
         // Add a custom payment gateway
         $gateways[] = 'custom_gateway';
         return $gateways;
     }
     ```
2. **`payment_page_settings`**
   * **Purpose**: Filters the settings before they are saved.
   * **Usage**: Validate or modify settings to ensure they meet specific criteria.
   * **Example**:

     ```php
     add_filter('payment_page_settings', 'sanitize_settings_data', 10, 1);

     function sanitize_settings_data($settings) {
         // Sanitize the API key setting
         $settings['api_key'] = sanitize_text_field($settings['api_key']);
         return $settings;
     }
     ```
3. **`payment_page_transaction_response`**
   * **Purpose**: Filters the response from payment gateways before it is processed by the plugin.
   * **Usage**: Modify response data for logging, notifications, or error handling.
   * **Example**:

     ```php
     add_filter('payment_page_transaction_response', 'process_transaction_response', 10, 1);

     function process_transaction_response($response) {
         // Modify the response to include additional data
         $response['processed_time'] = current_time('mysql');
         return $response;
     }
     ```
4. **`payment_page_form_fields`**
   * **Purpose**: Allows modification of form fields rendered on the payment page.
   * **Usage**: Customize the payment form by adding or removing fields based on user needs.
   * **Example**:

     ```php
     add_filter('payment_page_form_fields', 'add_custom_form_fields', 10, 1);

     function add_custom_form_fields($fields) {
         // Add a custom input field to the payment form
         $fields['custom_field'] = '<input type="text" name="custom_field" placeholder="Enter something...">';
         return $fields;
     }
     ```
5. **`payment_page_payment_methods`**
   * **Purpose**: Filters the list of payment methods available to users during checkout.
   * **Usage**: Add, remove, or modify payment methods based on specific criteria or conditions.
   * **Example**:

     ```php
     add_filter('payment_page_payment_methods', 'modify_payment_methods', 10, 1);

     function modify_payment_methods($methods) {
         // Remove 'check' as a payment method
         if (($key = array_search('check', $methods)) !== false) {
             unset($methods[$key]);
         }
         return $methods;
     }
     ```

***

This section provides advanced developers with a detailed understanding of the available hooks and filters in the **Payment Page** plugin, including practical examples of how they can be implemented. If you need further elaboration on any specific hook or filter, or if you have additional questions, feel free to [contact support](https://paymentpageplugin.com/support/).&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.paymentpageplugin.com/advanced-configuration/hooks-and-filters.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
