Skip to content

Call a custom function/method

The Call a custom function/method action, in the Run Code integration, calls any global PHP function or static method that exists on your WordPress site when a recipe runs. You name the function, pass it arguments in order, and the value it returns becomes the Function return value action token for the actions that follow. It is the quickest way to connect a recipe to code you have already written in a child theme or plugin. The action requires Uncanny Automator Pro.

Important: This action runs PHP code on your site. A fault in that code can make the site unusable, so use the action only if you are comfortable writing and debugging PHP, and test on a staging site before you use it on a live one.

Adding the action

  1. Open the recipe at Automator > Recipes, or create one from Automator > Add new recipe, and add its trigger; see Creating a recipe. This example uses the WordPress trigger A user views a page.
  2. Click the + at the end of the flow. In the picker, type Run Code in the search box and select Run Code, then Call a custom function/method.

The picker on its Action step for Run Code, listing the three Pro items Call a custom function/method, Run JavaScript code and Run a WordPress hook

  1. In the Set up step, enter the Function name, for example my_custom_function. The function can be defined in your child theme’s functions.php, in a plugin you wrote, or in any plugin installed on the site. As the field’s own hint says, “The function must be available or registered before this Automator action.” Once you enter a name, the sentence at the top of the step reads Call Function name: followed by it.
  2. Under Pass variables, click Add a variable once for each argument the function takes and enter the value, using the ∗ button to insert tokens where the value should come from the trigger. Variables are passed in the order listed, top to bottom. Skip this field for a function that takes no arguments. Only simple values are supported: arrays and objects are treated as strings, and null, [] and array() are passed as null and empty arrays.

The Set up step of Call a custom function/method with my_custom_function as the Function name, one URL under Pass variables, and the Cancel and Save buttons

  1. Click Save. This action has no Test step, so Save finishes it.
  2. Switch Recipe on in the top bar, then complete the trigger to run the function.

To use what the function returns, add another action after this one and insert the Function return value token from this action into one of its fields.

Writing a function

Writing PHP is beyond the scope of this article, but here is the example the steps above call. It redirects the user to the URL passed in Pass variables, for example https://sitename.com/?my_custom_url_param=yes (replace sitename.com with your own domain).

  1. Open your theme or child theme’s functions.php in a text editor.
  2. Paste in the following code and save the file:
function my_custom_function( $url ) {
    wp_safe_redirect( $url );
    exit;
}
  1. Visit the page you chose in the trigger. You should be redirected to the URL you passed.

Note: For a redirect on its own, the Redirect integration’s action does the same without code; see Working with redirects. The example is here because it is easy to see working.

Example: return a product’s meta value

/**
 * @param $product_id
 * @param $meta
 *
 * @return array|int|mixed|string
 */
function automator_get_product_meta( $product_id, $meta ) {

	// Get product
	$product = wc_get_product( $product_id );

	// if not correct Product ID, return 0
	if ( ! $product instanceof WC_Product ) {
		return 0;
	}

	// check if it's a variation
	$parent_id = $product->get_parent_id();

	// a variation, return parent product
	if ( 0 !== (int) $parent_id ) {
		$product = wc_get_product( $product->get_parent_id() );
	}

	// check again if its a product
	if ( ! $product instanceof WC_Product ) {
		return 0;
	}

	// return meta
	return $product->get_meta( $meta, true );
}

Example: run a custom MySQL query

/**
 * @param $user_id
 * @param $meta_key
 * @param $meta_value
 *
 * @return array
 */
function automator_run_mysql_query( $user_id, $meta_key, $meta_value ) {
	global $wpdb;

	// Update something.
	$wpdb->update(
		$wpdb->usermeta,
		array(
			'meta_value' => $meta_value,
		),
		array(
			'user_id'  => $user_id,
			'meta_key' => $meta_key,
		)
	);

	// Insert something
	$wpdb->insert(
		$wpdb->prefix . 'my-table',
		array(
			'user_id'    => $user_id,
			'meta_key'   => $meta_key,
			'meta_value' => $meta_value,
		)
	);

	// Return a value for the next action (Action token)
	return $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = %s AND user_id = %d", $meta_key, $user_id ) );
}

Troubleshooting

If the action does not do what you expect, check these in order:

  1. Recipe on is switched on, and the action and trigger are On.
  2. The Function name matches the function exactly. If it does not exist when the action runs, the action fails and the action log records “The function/method (…) you are trying to call is not found or not yet registered.”
  3. The number of entries under Pass variables matches the number of arguments the function takes.
  4. Your own code runs without PHP errors or warnings.

A mismatch in the name or argument count is reported in Automator > Logs on the Action tab. A fatal error inside the function itself, however, can stop the site from loading, so keep the Important note above in mind and test on a staging site first.

Related: Run a WordPress hook, Managing actions, Managing tokens

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top