Call a function

Call a function action lets you call to any function registered in your WordPress and pass the variables of your choice.

When you add a new action, you can find the call a function action under the AutomatorWP integration.

Next, click on the “a function” button and the action configuration will be displayed like this:

Simply, you need to enter the PHP function name to call and the variables you want to pass to this function and AutomatorWP will call it at the moment the automation gets completed!

In addition, you will have all information in logs to meet if the function has been executed correctly or if there was any error like incorrect parameters or an issue inside it’s code.

Note: If you do not know how to add your custom code to your WordPress, we recommend the plugin Code Snippets or a similar one that lets you add code to your site without edit your site files.

Example: Update a user meta with a conditional logic

Lets to create an automation to update a user meta with some conditional logic. For this one we will use the trigger “User completes a purchase” from WooCommerce to get the order total and update a user meta with the amount spent only if the order total is greater than $100.

The automation setup:

The function code:

function my_custom_function( $user_id, $order_total ) {

    // Ensure that user ID is a integer number and order total is a float number
    $user_id = (int) $user_id;
    $order_total = (float) $order_total;

    // Here you can add a conditional logic of your choice like:
    // - If the user has been referred by an affiliate
    // - Or if user has a specific role
    // - Or anything you want

    // Check if the order total is lower than $100, then do not update the meta
    if( $order_total < 100 ) return;

    // Get the meta value
    $already_spent = (float) get_user_meta( $user_id, '_total_spent', true );

    // Update the user meta adding the order total to the already spent amount
    update_user_meta( $user_id, '_total_spent', $already_spent + $order_total );

}

Example: Call to an existent function

In this example, lets to call to the WordPress function update_user_meta directly from the “Call to function” action. The update_user_meta function requires 3 variables: the user ID, the meta key and the value to assign (the previous value variable is optional).

The action setup:

Example: Call to call an existent function with complex variables

Some functions could require complex variables like arrays or objects that are not possible to pass through the “Call to function” action variables. For this kind of functions, the most easy way to solve this is by creating a custom function who receives the variables from the “Call to function” action and prepares them to be passed to the desired function.

In this example, lets to create a custom function that will receive information from AutomatorWP and will call to the wp_insert_post function converting the information from AutomatorWP into an array.

The action setup:

The function code:

function custom_create_post( $title, $content ) {

    // Build an array with the post information from the variables received
    $post_data = array(
        'post_title'    => $title,
        'post_content'  => $content,
        'post_author'   => get_current_user_id(),
        'post_status'   => 'publish',
    );

    // Insert the post
    wp_insert_post( $post_data );

}