tr Skip to content
Black Friday FlowMattic for life, 12 payments of $33.25 Or $399 once. Nothing added for splitting Ends in --Days --Hrs --Min --Sec See the deal

Run PHP in a Workflow: Call Your Own Functions From FlowMattic

FlowMattic PHP Functions step: a new order calls a custom PHP function that returns loyalty points and tier as workflow fields.

Sometimes a workflow needs one piece of logic no module covers: a loyalty-points formula, a lookup in a custom database table, a call into another plugin. The PHP Functions step lets you run PHP in a workflow by calling a function by name, passing it data from earlier steps, and handing whatever it returns to the next step.

It is built for developers and technical site owners who already have PHP on the site, in a custom plugin, a theme or a code snippets plugin, and want workflows to use it. You keep the code where code belongs and let FlowMattic decide when it runs and what data it gets.

What is the PHP Functions step?

PHP Functions by FlowMattic is a built-in action app with one event, Call PHP Function. It calls any PHP function that exists on your site when the workflow runs: a PHP built-in, a WordPress function, a function from another plugin, or one you wrote yourself. Static class methods work too, written as MyClass::my_method.

The step does not have a code editor. You write the function in PHP wherever you normally keep custom code, then name it in the step. That keeps your code in version control and out of the workflow’s settings.

How to run PHP in a workflow

  1. Make sure the function is loaded on every request, for example in a small custom plugin. A function loaded only on certain pages, or only in the admin, may not exist when a webhook or a scheduled run fires.
  2. In the workflow builder, add a step and choose PHP Functions by FlowMattic, then Call PHP Function.
  3. Enter the Function Name, such as acme_loyalty_points or Acme_Rewards::calculate.
  4. Choose Function Parameters Type: Single Array, Individual Variable or No Parameters.
  5. Under Function Parameters, add key and value rows with Add More, mapping values from the trigger or earlier steps.
  6. Set JSON Value Decode if any value is JSON, then test the step.
FlowMattic PHP Functions by FlowMattic step showing Function Name, Function Parameters Type, Function Parameters rows and JSON Value Decode.
The Call PHP Function step set to call a custom function with its parameters passed as a single array.

Here is a function written for the Single Array type. It receives one associative array built from your rows and returns an array, which becomes separate fields in the workflow:

function acme_loyalty_points( $args ) {
    $total  = isset( $args['order_total'] ) ? (float) $args['order_total'] : 0;
    $points = (int) floor( $total / 10 );

    return array(
        'points' => $points,
        'tier'   => $points >= 50 ? 'gold' : 'standard',
    );
}

With a row order_total mapped to the order total, the step returns points and tier as fields you can use in an email, a CRM update or a Router condition.

PHP Functions options

OptionWhat it does
Function NameA function name, or ClassName::method for a static method. The step checks it exists before calling it.
Function Parameters Type: Single ArrayPasses one argument: an array of all rows as key => value.
Function Parameters Type: Individual VariablePasses each row’s value as a separate argument, in row order. The keys are labels for you only.
Function Parameters Type: No ParametersCalls the function with no arguments and hides the parameter rows.
Function ParametersKey and value rows; values accept dynamic data.
JSON Value DecodePass as it is sends values as text. Convert JSON to Array turns any value that is valid JSON into a PHP array first.

What the step returns

  • An array or object: status is success, response holds the whole result as JSON, and each key also becomes its own field. Nested arrays are flattened with underscores, so address['city'] becomes address_city.
  • A string, number or boolean: status is success and response holds the value.
  • A PHP error: status is error and response holds the error message.
  • A name that does not exist: an error saying the function or method does not exist.

Use cases

Loyalty points on every order

Acme’s points rules live in one PHP function the whole site shares. The workflow passes the order total, gets back points and tier, and emails the customer their new balance.

Flow: WooCommerce New Order Created → Call PHP Function (acme_loyalty_points) → Send Email

Use a WordPress function directly

You do not have to write anything to use WordPress core. Call get_user_by with Individual Variable rows email and the submitted address, and the user’s details come back as fields a Router can check.

Flow: Form Builder New Form Submission → Call PHP Function (get_user_by) → Router

Flush the cache after publishing

An editorial team wants fresh pages the moment a post goes live. A No Parameters call to wp_cache_flush clears the object cache, then Slack confirms it.

Flow: WordPress New Post → Call PHP Function (wp_cache_flush, No Parameters) → Slack Send Channel Message

Reuse a client plugin’s logic

An agency built Northwind a reporting plugin with a static Northwind_Reports::weekly_summary method. The workflow calls it on a schedule and emails the result, with no duplicate logic.

Flow: Schedule Every Week → Call PHP Function (Northwind_Reports::weekly_summary) → Send Email

Validate a licence key and answer the caller

A SaaS product checks keys against a custom table. The webhook passes the key to a validation function, and its answer goes straight back as the webhook response.

Flow: Webhook → Call PHP Function → Webhook Response Set Webhook Response

Clean up AI output before it is stored

An AI agent drafts product descriptions, and a house-style function strips banned phrases and fixes casing before the text is saved to a WP Table for review.

Flow: AI Agent Execute Agent → Call PHP Function → WP Tables New Record

Tips and gotchas

  • Return an array. Arrays give you one mappable field per key; a single value only gives you response.
  • Static methods only. ClassName::method must be a static method. To use an object’s method, wrap it in a plain function.
  • Order matters for Individual Variable. Values are passed in the order of the rows, whatever the keys say.
  • Handle your own exceptions. The step catches PHP errors and records them, but write your function to catch exceptions and return a message so the run stays readable.
  • Testing runs the function. Test calls the real function with your sample data, so side effects happen.
  • Keep the name fixed. Function Name accepts dynamic data, but never map it from incoming data such as a webhook body. Type the name.
  • Limit who edits workflows. Anyone who can edit workflows can call any function on the site, so keep workflow access to people you trust with code.

Related features

  • PHP Array: sort, filter and search lists without writing a function.
  • API by FlowMattic: call external services over HTTP instead of PHP.
  • Variables: keep constants such as rates or IDs out of your code.

FAQ

Can I paste PHP code into a FlowMattic step?

No. The PHP Functions step calls a function by name. Put your code in a custom plugin, your theme or a code snippets plugin, then enter the function’s name in the step.

Can I call WordPress and plugin functions?

Yes. Any function that exists when the workflow runs can be called, including WordPress core functions and functions from other active plugins. Static class methods work with ClassName::method.

How do I pass several values to my function?

Choose Single Array to receive one associative array, or Individual Variable to receive each value as its own argument in row order. Set JSON Value Decode to Convert JSON to Array if a value is a JSON string you want as an array.

The PHP Functions step is one of the modules in the visual workflow builder. See FlowMattic pricing and run PHP in a workflow on your own server.

More from the blog