Often, you will need to use custom application logic for your Toolset forms. You can achieve this by using custom PHP solutions and the Toolset Forms API. The idea behind the Toolset Forms API is to allow you to do custom actions that are not otherwise possible through the user interface.
Toolset Forms API allows you to hook into different events of submitting a form. For example, you can add a custom action right before saving or processing any form data, but after the form was validated.
Or, you could add a custom type of an event that triggers notifications. The possibilities are limitless.
To use the Toolset Forms API, you will need a good knowledge of PHP.
Adding custom PHP from the WordPress admin
Toolset allows you to add any custom PHP code directly from the WordPress admin. This is great because you don’t have to create a child theme to add custom PHP code. You also don’t need to edit any PHP files.
Use the following steps to add custom PHP code to your site:
- Go to the Toolset → Settings page and click the Custom Code tab.
- To add a code snippet, click the Add new button, type in the name (slug), and click Create.
- Your snippet is created and listed on the page. Click on its name to edit its options and add code to it.
Check out the related page to learn more about using Toolset to add custom code.
Examples of using the Forms API
To help you get started, let’s take a look at some of the common uses of Forms API.
Create a post when registering a user
After using a Toolset User form to register a user, you might need to automatically create a custom post for this new user. For example, when someone registers on the site about events, you could create a separate post in the „attendees“ post type.
To achieve this, you can use the cred_save_data API hook:
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data) { if ($form_data['id']==9999) { // replace with your form's ID { // Create new entry after registration. $my_post = array( 'post_title' => 'Attendee', // adjust the title 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, // adjust post_author if/as required 'post_type' => 'attendees' // replace with the slug of your post type ); // Insert the post into the database wp_insert_post( $my_post ); } }
You will need to adjust the following:
- Replace 9999 with the ID of your user form for registering users.
- Replace the post type name „attendees“ with the slug of your post type.
Set a post taxonomy automatically when a post is created
When a user submits a new post from the front-end, you might want to set a specific, predefined taxonomy for that post. For example, you could have multiple categories but you only want to allow submitting a „report“ post.
To achieve this, you can use the cred_save_data API hook in combination with the core has_term function:
add_action("cred_save_data", "attach_default_terms",10,2); function attach_default_terms($post_id, $form_data) { // if a specific form if ($form_data["id"] == 9999) { // replace with your form's ID if(!has_term('','category',$post_ID)) { $category_ids = array(6); // replace with your category ID wp_set_object_terms( $post_id, $category_ids, 'category'); } wp_update_post( $my_post ); } }
You will need to adjust the following:
- Replace 9999 with the ID of your form.
- Change the $category_ids array to contain the IDs of the taxonomy terms you want to assign to the post created by the form.
Additionally, in this example, our form is set to submit standard WordPress posts. This is why we’re checking for the „category“ taxonomy using the has_term function. If you need to check for custom taxonomy, simply replace „category“ with the slug of your taxonomy.
Connect the submitted post to a predefined parent post
When a user submits a new post using a form, you might want to automatically connect it to a predefined parent post.
To achieve this, you can use the toolset_connect_posts function:
add_action('cred_save_data','func_connect_child_posts',15,2); function func_connect_child_posts($post_id,$form_data) { if ($form_data['id']==999999) // replace with your form's ID { toolset_connect_posts('relationship-slug',$_POST['@relationship-slug.parent'], $post_id); // replace with the slug of your actual relationship } }
You will need to adjust the following:
- Replace 9999 with the ID of your form.
- Replace relationship-slug with the actual slug of your relationship.
Finally, edit your form and remove the parent field if it’s already added. Then, add a Generic field that will hold the parent post’s ID.
[cred_generic_field field='@relationship-slug.parent' type='hidden' class='' urlparam=''] { "required":0, "validate_format":0, "default":"[wpv-post-id]" } [/cred_generic_field]
You will need to adjust the following:
- Replace relationship-slug with the actual slug of your relationship.
Further documentation
To add custom functions to Toolset forms you will need the page about Forms API Hooks.
We also recommend reading our comprehensive guide for getting started with Toolset hooks.
Related topics
- Using WooCommerce Fields in Product Forms
- Selecting Parent Posts when Using Forms to Create Child Items
- Displaying Form Data
- Allow users to manage their items
- Charge users for submitting content
- Styling Front-End Forms
- Setting up Automatic Post Expiration
- Controlling Access to Front-End Forms
- Adding Generic Fields to Forms
- Send Notifications when Someone Submits the Form
- Deleting Content from The Front-End
- Front-end Forms for Editing Content
- Front-end Forms for Adding Content
- Using the Native Media Manager in Front-End Forms
- Protecting Forms from Spam
- Front-end Forms For Repeatable Field Groups
- Selecting allowed tags in front-end forms
- Letting Forms Edit Post and User Fields Created by Other Plugins
- Conditional Display for Form Inputs
Need help?
If you bought Toolset, log-in to your account and post your questions in Toolset technical support.
Not a client yet? Find the account that’s best for you.