From this tutorial, you will get knowledge of how to work with Hidden and Calculated field types to create math formulas that your visitors could use for their Forms on the frontend.
Hidden and Calculated field types
Let’s clear up what Hidden field type means. In fact, the name speaks for itself. You may add the value to this field that won’t be visible on the frontend, but can be used for calculations.
Calculated fields help you to use formulas. Using it, you can make any calculations using math formulas based on macros and display the result on the frontend. The values for the formulas you can take either from meta fields created previously for your custom post or from the fields filled on the front end by users.
Using Hidden and Calculated field types
Let’s consider an example of using these fields. We’ll create a short Form for a product purchase.
1 Step — Adding Hidden field
First of all, create fields to display the product’s Price:
Now, it is time for the Hidden field. In this tutorial, we’ll create a field for Tax, which further will be added to the final price, so that on the frontend visitors won’t see Tax field separately, but its value will be considered in the formula calculating Total Price.
Create a new field and select Hidden from the dropdown list in the Type:
and input Name value:
Next, in the Field Value select Current Post Meta and input meta field name ID:
This value stands for pulling data from your Custom Post Type meta field. Let’s see where it can be located:
2 Step — Adding Calculated field
Now, let’s create a Calculated field and see how the Hidden field can be used there. We assume this field’s purpose is to calculate the total price of the product, including tax. Create a new field and select its type as Calculated and input a name for it.
In the Calculation Formula field, you need to input a math formula, which will further calculate the product’s total price depending on the quantity a visitor selects. Plus, we need it to consider tax and summarize the final price accordingly.
In this particular case for our tutorial, our Calculation Formula consists of Product Quantity multiplied by the Product Price plus Tax:
%FIELD::Tax% is a Hidden field type.
Add Calculated Value Prefix, which will be displayed on the frontend, and click the “Apply Changes” button:
When your Form is ready, click the “Publish” button:
Let’s see how it works on the frontend:
Price and Quantity fields are visible, and Tax field is not, however, in the Total calculation formula its value is considered.
If we describe it as a math formula:
20 (Price) * 1 (Quantity) + 5 (Tax (hidden field)) = 25
If a visitor changes Quantity, the formula still calculates the Total Price considering tax value:
That’s all! Now you know how to use Hidden and Calculated field types to create formulas based on the values from the form fields. Enjoy managing your forms and create them effortlessly!