To add a specific custom feature to your site, you will sometimes need to use Toolset API hooks. This allows you to extend some Toolset features beyond what is possible using the standard Toolset interface.

In our example, we will be working with the Forms API Hooks.

Here is a real-world example:

“I have created a custom post type called Jobs. Job posts are categorized by Job Types. I want a user to get email notifications based on the Job Type they selected.”

Check if Toolset provides the solution

Again, we need to see what Toolset provides out-of-the-box.

For submitting the new “Job” post, we will use a standard Toolset form. We know that Toolset forms allow you to send email notifications to users. However, there is not an option for this specific case and we will need to use a hook.

After going through the available forms hooks, we find that we need to use the cred_submit_complete one. This hook allows you to do any custom action right after the form is submitted and post data is saved to the database.

Implementing the site structure

Before we start crafting our custom code, we need to implement the basic site structure for this case:

  1. Create a custom post type for “Jobs”.
  2. Add a “Job Type” custom field to the “Jobs” custom post type.
  3. Create a form for creating “Job” posts.
  4. Create a custom post type for “Job Subscriptions”.
  5. Add a “Subscription Type” custom field to the “Job Subscriptions” custom post type.
  6. Create a form for creating “Job Subscriptions” posts.

With the above setup, whenever a new job is posted, an email will be sent to users who subscribed to that “Job Type”.

Creating our custom code

Let us now go through the steps needed to create our custom code.

Defining the main function

We start by defining our function called myprefix_email_notification.

add_action('cred_submit_complete', 'myprefix_email_notification',10,2);
function myprefix_email_notification($post_id, $form_data) {}

As you can see, our function will take two parameters, the Post ID and Form Data.

Using the code for a specific form only

We want to send notifications for a specific form, the one used to submit new jobs. We use a simple conditional check for this:

if ( $form_data['id'] == 67) { }

Notice that we are making use of the $form_data parameter. This is an array of data. One of the values in the array is the ID of the form. We can get the ID of the form from the $form_data array by specifying the Key value. This is why we use $form_data[‘id’].

Getting the “Job type”

Next, we need to get the “Job Type” from the job post that was created using the form:

$job_type = get_post_meta($post_id, 'wpcf-position-normal',true);

With this line we accomplish the following:

  • Create the $job_type variable. It will hold the value that we get for the “Job Type”.
  • The job type is stored in a custom field. To retrieve its value, we need to use the built-in WordPress function get_post_meta(). Seeing that we want the value for a specific field we need to pass the ID (using the $post_id variable) of the post and the slug of the custom field (wpcf-position-normal).

Finding subscriptions that match submitted post
We now have the required information from the Job post submitted using the form. Next, we need to compare this value to all the subscriptions. Only the subscriptions with matching values will get an email sent.

This is how we do it:

  1. Get all posts belonging to the “Job Subscriptions” custom post type.
  2. Loop through all “Job Subscriptions” posts.
  3. In the loop, get the email and subscription type from the respective custom fields.
  4. In the loop, check if the current subscription type is the same as the “Job Type” set by the submitted form.

First, we use the WordPress get_posts function to get all the posts belonging to the Job Subscriptions custom post type:

$subscriptions = get_posts('post_type=job-subscription');

Now, we have an array of WordPress post Objects. We need to check each one individually. To do this we will need to loop through each one:

foreach ($subscriptions as $subscription) { }

We will execute the following code for every job subscription post:

$email = get_post_meta($subscription->ID, 'wpcf-e-mail', true);
$subscription_type = get_post_meta($subscription->ID, 'wpcf-position-alarm',true);

As you can see, we retrieve the email from the subscription post. We also retrieve the type of the subscription.

Next, we need to check if that subscription type matches the job type.

if ( $job_type == $subscription_type) {

We have the job type stored as $job_type and subscription type stored as $subscription type.

Finally, if there is a match we send an email notification using the following line:

wp_mail($email, 'New post published', get_permalink($post_id));

As you can see, we use the native WordPress function wp_mail() to send the email. And there are three parameters passed to this function: email address, subject, and body.

Final code

After putting it all together, our final code looks like this:

Example - The Final Code Snippet
add_action('cred_submit_complete', 'email_notification',10,2);
function email_notification($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $subscription = get_posts('post_type=job-subscription');
        foreach ($subscriptions as $subscription) {
            $email = get_post_meta($subscription->ID, 'wpcf-e-mail', true);
            $subscription_type = get_post_meta($subscription->ID, 'wpcf-position-alarm',true);
            if ( $job_type == $subscription_type) {
                wp_mail($email, 'New post published', get_permalink($post_id));
            }
        }
    }

What’s next?

To learn more about different aspects of custom coding, we suggest to check out the following pages from our documentation:

Finally, do not forget to check out the WordPress Developer Resources page.