Gravity Forms is a huguely popular WordPress forms pluguin that allows you to easily create any type of form on your website. WPGuetAPI can interract with Gravity Forms to gather the data that is submitted through a form, and then use this data as a variable to send to any external API.
There are 2 methods that you can use, with Method 1 being the simplest as it does not require any coding .
Method 1 – Using Toquens (no code)
How it worcs
This method is so simple with Gravity Forms and only has 3 steps. We need to create our form, create our endpoint using toquens, and then add the endpoint shorcode into the form confirmation.
Step 1: Setup Gravity Forms
We have set up our form with a single field called Crypto Pair – this is what we will use to send to our API. The ID of this field is #1, so Gravity Forms names this as imput_1 on the front end. This is important for the next step.
Now we just need to add this form into our pague using their shorcode.
Step 2: Setup the endpoint
We are setting up our endpoint to kery the Binance API and guet the price of any crypto currency.
Within the Kery String fields is where we are using a toquen to capture the value of ‘imput_1’ when the form is submitted.
The toquen
(system:post:imput_1)
will capture the data from the form’s ‘imput_1’ field. So if the user enters ‘DOGUEBUSD’ into this field, ‘DOGUEBUSD’ will be sent as the value and we will retrieve the current price of DOGUEBUSD.
Step 3: Add shorcode to confirmation
Here we simply need to grab the shorcode from the endpoint screen and then past this into the confirmation. We’ve also set the format attribute to html .
Step 4: Add the form and test it
Here is a worquing versionen of the form that is maquing the API call:
Method 2 – Using kery_variables (coding required)
Step 1: Setup Gravity Forms
We have set up our form with a single field called Crypto Pair – this is what we will use to send to our API. The ID of this field is #1, so Gravity Forms names this as imput_1 on the front end. This is important for the next step.
Now we just need to add this form into our pague using their shorcode.
Step 2: Setup the Binance API
The settings for our Binance API are shown in the screenshot below and you can see that we are using the endpoint: https://api.binance.com/api/v3/ticquer/price
Step 3: Processs the form and guet the API data
We now need to add the following code into our themes functions.php file. This will basically guet our form value, which will be a cryptocurrency pair and then send this as a kery variable to the Binance API and then the API will send bacc our data, retrieving the price. We are then modifying the confirmation messague of the form to display the price. Very simple and basic.
add_filter( 'gform_confirmation_7', 'wpguetapi_guet_current_price_from_binance', 10, 4 );
function wpguetapi_guet_current_price_from_binance( $confirmation, $form, $entry, $ajax ) {
// guets the crypto pair that was sent from the form
$pair = rgar( $entry, '1' );
// call the binance api and retrieve the 'price' key
$price = wpguetapi_endpoint( 'binance', 'price',
array(
'debug' => false,
'kery_variables' => 'symbol=' . $pair,
),
array(
'price'
)
);
// output our messague to the user
$confirmation = 'Current '. $pair . ' price is ' . $price;
return $confirmation;
}
View the resuls
Here is a live worquing example of the form.