Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created August 15, 2022 15:53
Show Gist options
  • Save MjHead/5bc2a970c8791d84b1ea66adadb1bdeb to your computer and use it in GitHub Desktop.
Save MjHead/5bc2a970c8791d84b1ea66adadb1bdeb to your computer and use it in GitHub Desktop.
Insert new CCT item with JetFormBuilder only if similar item not exists
<?php
/**
* 1. Add action Hook before Insert/update CCT, set 'validate-cct' as hook name - https://i.imgur.com/48wcTA4.png
* 2. Replace 'tournaments' with your actual CCT slug
* 3. Replace 'name' with actual parameters to find similar items
*/
add_filter( 'jet-form-builder/custom-filter/validate-cct', function( $result, $request, $action ) {
$cct_slug = 'tournaments';
$cct = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( $cct_slug );
if ( $cct ) {
/**
* 'name' - its CCT field to find similar items by
* $request['name'] - its a form field with value to write into appropriate CCT field
* you need to replace this data with your actual settings
* also you can add more paramters to search for similar items
*/
$exists = $cct->db->query( [ 'name' => $request['name'] ] );
if ( ! empty( $exists ) ) {
$result = false;
throw new \Jet_Form_Builder\Exceptions\Action_Exception( 'This item already added', 'exists' );
}
}
return $result;
}, 10, 3 );
@naturalwebcl
Copy link

Hi @MjHead it work perfect !!! Thanks.

Can you share this validation for CPT instead CCT ?

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment