Articles in this section
Category / Section

How to Use the PHP Functions Module in FlowMattic Workflows

2 mins read
Updated:

The PHP Functions module in FlowMattic allows you to enhance your workflows by integrating custom or predefined PHP functions. This module is particularly useful when existing modules are insufficient for your requirements or when you need to perform advanced operations. Here’s a detailed guide to help you get started:

Steps to Use the PHP Functions Module

  1. Write or Add Your PHP Function

    • Create a custom PHP function or use a predefined one.
    • Insert your custom function into your site using plugins like Fluent Snippets or similar tools.
    • Ensure the function is publicly accessible. For class-based functions, ensure they are static (e.g., MyClass::my_function).
  2. Configure the PHP Functions Module

    • Function Name: Enter the name of your PHP function. For example, use my_function for standalone functions or MyClass::my_function for static class functions.
    • Function Parameters Type: Select how to pass parameters:
      • Single Array: Combines all parameters into a single array.
      • Individual Variables: Passes each parameter as a separate variable.
      • No Parameters: Use this if your function does not require any parameters.
    • Function Parameters: Add the required parameters as key-value pairs. FlowMattic provides an intuitive UI for this. For example, you can pass parameters as:
      array(
          "key1" => "value1",
          "key2" => "value2"
      )
      
  3. Handle JSON Data (Optional)

    • If your function requires JSON data, use the JSON Value Decode option to convert the JSON input into a PHP array. Set this option to Yes to enable decoding.

Tips for Best Practices

  • Always test your PHP function independently before using it in a workflow to ensure it behaves as expected.
  • Use descriptive parameter keys to make your workflows easier to debug and maintain.
  • If you’re using third-party plugins for adding custom PHP functions, ensure they are compatible with your WordPress version and FlowMattic.

Example Workflow

Suppose you have a custom function named calculate_discount that calculates a discount based on an order total and a discount percentage. Here’s how you can configure the PHP Functions module:

PHP Functions module configuration

  • Function Name: calculate_discount
  • Function Parameters Type: Individual Variables
  • Function Parameters:
    order_total => 100
    discount_percentage => 10
    
  • JSON Value Decode: No

When executed, the calculate_discount function will process the parameters and return the calculated discount value.

By leveraging the PHP Functions module, you can greatly expand the capabilities of your workflows, making them more dynamic and tailored to your specific needs.

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Access denied
Access denied