PRO Plugin – Filters & Action Hooks
These actions and filters help extend or modify the functionality of the PRO plugin. Code snippets would need to be added to your themes functions.php file.
Filter: wpgetapi_query_variables_delimiter
This filter allows you to modify the delimiter when using the query_variables option as shown here. The default delimiter is a comma but sometimes commas are accepted within a query string.
function wpgetapi_modify_delimiter( $delimiter ) {
// set the delimiter to whatever you like
return '&';
}
add_filter( 'wpgetapi_query_variables_delimiter', 'wpgetapi_modify_delimiter', 10, 1 );
Filter: wpgetapi_line_items_wrapper
This filter allows you to modify how each line item is wrapped when using the (line_items) token as shown here. The default wrapper looks like this: {value},
The example below will remove the curly braces.
function wpgetapi_modify_line_items_wrapper( $value ) {
$value = str_replace( '{', '', $value );
$value = str_replace( '}', '', $value );
return $value;
}
add_filter( 'wpgetapi_line_items_wrapper', 'wpgetapi_modify_line_items_wrapper', 10, 1 );
Filter: wpgetapi_query_variables_explode_separator
This filter allows you to modify the explode separator when using the query_variables option as shown here. The default delimiter is an equals sign = but sometimes this may need to be changed.
function wpgetapi_modify_explode_separator( $separator ) {
// set the separator to whatever you like
return '~';
}
add_filter( 'wpgetapi_query_variables_explode_separator', 'wpgetapi_modify_explode_separator', 10, 1 );
Filter: wpgetapi_argument_values_before_{$action}
This filter allows us to modify a value from an Action before it is sent to the API. The {$action} is the name of the action being run, which can be found in the Actions Log.
In this example we have the Action set to WooCommerce – New Order Created. So looking below at the code, we can see that the {$action} has been set as: wpgetapi_argument_values_before_woocommerce_new_order
. This means that this code will only execute when a new WooCommerce order is created.
The code below is modify the value of ‘number’ and also rounding each of the line item subtotals to 2 decimal points.
function wpgetapi_modify_action_values( $args, $endpoint ) {
// remove INT- from 'number'
if( isset( $args['number'] ) )
$args['number'] = str_replace('INT-', '', $args['number'] );
// round each line item subtotal
if( isset( $args['line_items'] ) ) {
foreach ( $args['line_items'] as $i => $line_item ) {
if( isset( $line_item['subtotal'] ) )
$args['line_items'][ $i ]['subtotal'] = round( $line_item['subtotal'], 2 );
}
}
return $args;
}
add_filter( 'wpgetapi_argument_values_before_woocommerce_new_order', 'wpgetapi_modify_action_values', 10, 2 );
Action: wpgetapi_after_{$action}
This filter runs immediately after a WPGetAPI Action is run. Actions are set within the admin as shown below. The {$action} is the name of the action being run, which can be found in the Actions Log.
In this example we have the Action set to call the API whenever the Gravity Forms – Lead Capture Form is filled in. The ID of this form is #8. So looking below at the code, we can see that action is: wpgetapi_after_gravity_forms_8
. This means that this code will only execute when this form is submitted.
We are using the response code of the API call to then redirect based on this response code.
function wpgetapi_redirect_after_response( $result, $argument_values, $values_sent, $endpoint, $response_code ) {
// if successful 200 response, do a redirect
if( $response_code == 200 ) {
$url = home_url( '/confirmation' );
wp_redirect( $url );
exit;
}
// if failed 404 response, do a redirect
if( $response_code == 404 ) {
$url = home_url( '/error' );
wp_redirect( $url );
exit;
}
}
add_action( 'wpgetapi_after_gravity_forms_8', 'wpgetapi_redirect_after_response', 10, 5 );