In this tutorial, you will learn how to customize CRED forms.
In Part 1, we created a form using the Auto-Generate Form feature. Now, we will edit the form to have it function properly and with a desirable appearance.
Introducing the form with custom text
Let us begin by introducing our form with the following simple instruction: Fill out the form to place your free ad.
This instruction will be part of our form, so we will insert it into the layout as a new row.
In your CRED Training Site:
- Open the page that contains our form. The page in our example is entitled Place your free ad.
- Open the content layout for editing.
- In the Layouts editor, click on the plus icon on the right to add a new row.
- Draw a full width cell.
- Select the Visual Editor cell.
- Insert the following text: “Fill out the form to place your free ad.”
- Click Apply.
- Grab your row and drag it to a position before the row containing our form.
- Preview the layout.
You should see the text you have added (“Fill out the form to place your free ad”) above your form.
You can display different introductory text for both logged-in and non-logged-in users. For this, use a Visual Editor cell, the conditional output feature, and the wpv-current-user shortcode as shown in the following screenshot.
Now let us modify the form.
- Click on the cell containing the form to open it for editing.
- Scroll down to the Content section.
You can edit everything between the [credform class=’cred-form cred-keep-original’] and [/credform] shortcodes.
Changing Input Field Labels
Each of the input fields included in your form has a label. By default the labels are based on what you set in the Types plugin.
For the Ad post title and body CRED inserts the “Ad Name” and “Ad Description” labels, respectively. The labels of your Post Fields are exactly the same as you set in Types.
We want to change the labels and add additional descriptions as well as some custom styling:
- Locate the line with “Ad name” label
- Edit the line to replace “Ad name” with the following code:Title <span>Keep it short and simple</span>
Repeat the steps above to replace the following labels:
|Ad Name||Title <span>Keep it short and simple</span>|
|Ad Description||Ad Content <span>Ideally two or three paragraphs</span>|
|Price||Price ($) <span>Your product’s price</span>|
Preview your form. New labels appear; however, they may need some changes. We achieve this using custom CSS.
Adding Custom Styling
We add some simple CSS to improve label formatting. We want the phrases that are wrapped in tags to begin from a new line.
- Scroll down to the bottom of your form editor.
- Insert the following CSS rules.
Use the Preview button to preview your changes. Your labels should be bolded and followed by additional descriptions starting from a new line.
Once you are satisfied with your changes press the Update button to save your form.
Removing the Group Field Title
When you generate your form with the Auto-Generate option, CRED automatically adds the name of your custom fields group. In our case, it reads “Ad details.” Now, let us remove it.
1. In your form body locate the following line:
<div class=”cred-header”><h3>Ad details</h3></div>
2. Remove it
Customizing the Category Block
When you generate your form with the auto-generate option, CRED automatically adds your taxonomy terms as a list of checkboxes and the “Add new” button allowing you to add a new term. See the Before picture below.
We remove the “Add new” button and turn the list of terms into a dropdown.
Removing the “Add new” button
- Locate the following block:
- Remove it
Changing the checkboxes into a dropdown
- Locate the following shortcode:
Category as checkboxes
[cred_field field='ad-category' display='checkbox']
- Replace the display=’checkbox’ with display=’select’ single_select=’true’. Your shortcode displaying taxonomy terms should look like this
Ad category as a dropdown
[cred_field field='ad-category' display='select' single_select='true']
Changing the category label
- Locate the following HTML block: <div class=”cred-header”><h3>Ad categories</h3></div>
- Replace it with the following block:
Changing the Sequence of Input Fields
Let us move our category dropdown up, to the very beginning of our form.
- Locate the block for category:
<div class="cred-group cred-group-taxonomies"> <div class="cred-taxonomy cred-taxonomy-ad-category"> <label class="cred-label">Category</label> [cred_field field='ad-category' display='select' single_select='true'] </div> </div>
- Select it
- Cut-and-paste it to the place before the Ad Title
- Preview your changes
- Once you are satisfied with the result, save your form
When moving input fields, remember to move the entire block of code that comprises that field, including its wrapping divs and labels.
Your form should look like this:
Validating the Email Field
You can set any field as “required.” For some fields, CRED can also validate the correct format. Let us add some validation to the Contact email field.
We set the field as required and check whether that which a user enters resembles an actual email.
Form input fields that are based on Post Fields inherit the validation set for fields in the Toolset Types plugin. To add some basic validation to the Contact email field, we must edit that field in Toolset Types.
- Open the site in a new tab so that you can quickly return to the layout later.
- Go to Toolset->Post Fields.
- Locate the Ad details post field group and open it for editing
- Click the Contact email field to expand it
- Locate the Validation section.
- Check the Required field to set the email field as mandatory. You can also replace the default message with one you have customized.
- Check the Email field for a valid email. Again, you may replace the default message with your customized version.
- Save the group.
- Return to the tab containing your layout. Click the Save and Close this form and return to the layout.
- Update the layout to save your changes.
- View your page to test the form again.
Testing how the validation works
- Leave the email field empty and submit the form. A message will display indicating that the field is required.
- Enter a random string in the email field and submit the form again.
This time a message will appear requesting a valid email address.
In this tutorial, you have learned how to customize your form.
- You can fully customize a form with markup included between the [credform class=’cred-form cred-keep-original’] and [/credform] shortcodes.
- You can change the order of your form fields and their labels including that of the submit button.
- You can modify the appearance of some of your form fields by changing the shortcode attributes, just as we did convert the category list from checkboxes to a dropdown list.
- Form input fields that are based on Post Fields inherit the validation set for fields in the Toolset Types plugin.
Need help? Questions?
Go to the CRED Course Support page and leave your comment. Agnes, the tutorial’s author, will reply to you.
In the next tutorial, you will learn what Generic Fields and Conditional Groups are and how to add them to your forms.