Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created June 25, 2024 10:52
Show Gist options
  • Save Lonsdale201/86af4a303fbb5a236985ddf595c5a05a to your computer and use it in GitHub Desktop.
Save Lonsdale201/86af4a303fbb5a236985ddf595c5a05a to your computer and use it in GitHub Desktop.
JetFormBuilder - Call hook - Create FluentCRM List
// place the code in the child theme functions.php or a custom code snippets plugin, like FluentSnippets
// What do this code
// Using a call hook in the JetFormBuilder, if the form is submitted successfully,
// a new list is created in fluentcrm. The list name is given by the value of a text field of your choice.
// If there is already a list with this name, the hook will not be triggered, i.e. it will not be overwritten or modified.
// HOW TO USE
// Just rewrite the marked parts of the code to take the value from the text field of your choice (you need the form field name)
use Jet_Form_Builder\Exceptions\Action_Exception;
add_filter('jet-form-builder/custom-filter/crm-create-list', function($result, $request, $action_handler) {
if (!function_exists('fluentCrmDb')) {
return $result;
}
// Check if the target field exists in the request
if (isset($request['list_name_jfb'])) { // Change 'list_name_jfb' to your target field name
$list_name = sanitize_text_field($request['list_name_jfb']); // Change 'list_name_jfb' to your target field name
// Check if the list already exists
$existing_list = fluentCrmDb()->table('fc_lists')->where('title', $list_name)->first();
if ($existing_list) {
return $result; // If the list exists, do nothing
}
// Create a new list
try {
$new_list_id = fluentCrmDb()->table('fc_lists')->insertGetId([
'title' => $list_name,
'slug' => sanitize_title($list_name),
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
]);
if ($new_list_id) {
return $result; // Return the original result if creation is successful
} else {
throw new Action_Exception(
__('Error creating the new list in FluentCRM.'),
'failed'
);
}
} catch (Exception $e) {
throw new Action_Exception(
__('An unexpected error occurred while creating the list.'),
'failed'
);
}
}
return $result;
}, 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment